Я знаю, что этот вопрос задавали много раз, но я не нашел, почему происходит нулевое исключение. spinnerCourses - это идентификатор Spinner, и когда я пытаюсь установить адаптер, я получаю нулевое исключение.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val dm = DataManager()
val adapterCourses = ArrayAdapter<CourseInfo>(this, android.R.layout.simple_spinner_item,
dm.courses.values.toList())
adapterCourses.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinnerCourses.adapter = adapterCourses
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
}
А вот и журнал ошибок ::::::::::::::::::::::::::::: ::::::::::::::::::::::::::
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.notekeeper/com.example.notekeeper.MainActivity}: java.lang.IllegalStateException: spinnerCourses must not be null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IllegalStateException: spinnerCourses must not be null
at com.example.notekeeper.MainActivity.onCreate(MainActivity.kt:26)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Код макета ::::::::::::: :::::::::::::::::::::::::::::::::::
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstFragment">
<EditText
android:id="@+id/textNoteText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:gravity="start|top"
android:hint="Note text"
android:inputType="textMultiLine"
app:layout_constraintEnd_toEndOf="@+id/spinnerCourses"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/spinnerCourses"
app:layout_constraintTop_toBottomOf="@+id/textNoteTitle" />
<Spinner
android:id="@+id/spinnerCourses"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/textNoteTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:gravity="start|top"
android:hint="Note title"
android:inputType="textMultiLine"
app:layout_constraintEnd_toEndOf="@+id/spinnerCourses"
app:layout_constraintStart_toStartOf="@+id/spinnerCourses"
app:layout_constraintTop_toBottomOf="@+id/spinnerCourses" />
</androidx.constraintlayout.widget.ConstraintLayout>