Пытаюсь создать recyclerview
в андроид студийном проекте. Однако, когда я пытаюсь протестировать проект, я сталкиваюсь с ошибкой,
D/Diagnise: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
Сначала я вложил RecyclerView
в RelativeLayout
и столкнулся с исключением. Затем изменил весь макет на RecyclerView
.
пакет smartpoultry.cocis.smartpoutry
import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.*
import android.widget.TextView
private val TAG : String = Diagnise ::class.java.simpleName
class Diagnise : AppCompatActivity() {
private lateinit var recyclerView : RecyclerView
private lateinit var adapterView : RecyclerView.Adapter<*>
private lateinit var viewManager : RecyclerView.LayoutManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_LEFT_ICON)
val actionBar = supportActionBar
actionBar!!.setDisplayHomeAsUpEnabled(true)
actionBar!!.title = "DIAGNOSIS"
setContentView(R.layout.activity_diagnise)
try {
populateSymptoms()
} catch(e : Exception) {
Log.d(TAG, e.message)
}
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when(item?.itemId) {
android.R.id.home -> {
onBackPressed()
return true
}
else -> return super.onOptionsItemSelected(item)
}
}
fun getUserName() {
val userDetails = getSharedPreferences("user_details", Context.MODE_PRIVATE)
val userID = userDetails.getLong("userID", -1)
val userData = ExtendsDatabaseHandler(this)
//statNotificationMessage?.text = "Hi " + userData.getUserDataById(GetUserNameModel(userID)) + "Error occured."
}
fun populateSymptoms() {
var dataSet = mutableListOf("One", "Two")
viewManager = LinearLayoutManager(this)
adapterView = MyDataAdapter(dataSet)
recyclerView = findViewById<RecyclerView>(R.id.scrollable).apply{
//setHasFixedSize(true)
layoutManager = viewManager
adapter = adapterView
}
}}
Адаптер
class MyDataAdapter(private val data : MutableList<String>) : RecyclerView.Adapter<MyDataAdapter.ViewHolder>() {
inner class ViewHolder (view : View) : RecyclerView.ViewHolder(view) {
//Get view reference
var textViewId = view.findViewById<TextView>(R.id.symptomTextData)
}
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
// create view holder to hold reference
return ViewHolder(LayoutInflater.from(p0.context).inflate(R.layout.symptoms_lay_out, p0, false))
}
override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
//Lets add data to our reference
p0.textViewId.text = data[p1]
}
//Getting size of our data.
override fun getItemCount() = data.size}
// Мои макеты
//activity_diagnise.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:backgroundTint="#96FFFFFF"
android:backgroundTintMode="src_over"
android:id="@+id/scrollable"
android:background="@drawable/splash_bg_2"
>
//symptoms_lay_out.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="@string/symptomsData"
android:layout_width="0dp"
android:layout_height="92dp" android:id="@+id/symptomTextData" android:layout_weight="1"/>
</RelativeLayout>
</android.support.v7.widget.RecyclerView>
Я ожидаю некоторого вывода на экран. Большое вам спасибо