0
votes

I am a beginner at programming. I have an error message when I start my app and do not know what to do with the code. Can someone help me please.

Here is my code:

Activity :

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val items = ArrayList<String>()
        items.add("Test1")
        items.add("Test2")
        items.add("Test3")

        listview1.adapter = Adapter1(this, items)

        val transaction1 = supportFragmentManager.beginTransaction()
        fragment1Button1.setOnClickListener{

            transaction1.replace(R.id.fragment1Platz,Fragment1())
            transaction1.commit()
        }

    }
}

Fragment :

class Fragment1 : Fragment() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.fragment_fragment1, container, false)
    }


}

Adapter:

class Adapter1 (context : Context, items : ArrayList<String>) : BaseAdapter() {


    private val context: Context = context
    private val items: ArrayList<String> = items


    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

        val listeneintrag = LayoutInflater.from(context).inflate(R.layout.adapter1_layout,parent, false)

        listeneintrag.findViewById<TextView>(R.id.adapter_textfeld1).text = items.get(position)

        return listeneintrag
    }

    override fun getItem(position: Int): String {
        return items.get(position)
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return items.size
    }
}

Error stacktrace:

2020-02-01 13:37:17.972 10729-10729/? E/AndroidRuntime: FATAL EXCEPTION: main Process: strauss2002.yannick.fragmentetest, PID: 10729 java.lang.RuntimeException: Unable to start activity ComponentInfo{strauss2002.yannick.fragmentetest/strauss2002.yannick.fragmentetest.MainActivity}: java.lang.IllegalStateException: listview1 must not be null at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.IllegalStateException: listview1 must not be null at strauss2002.yannick.fragmentetest.MainActivity.onCreate(MainActivity.kt:29) at android.app.Activity.performCreate(Activity.java:7009) at android.app.Activity.performCreate(Activity.java:7000) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Here is my updated Main Activity:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val listview1=findViewById<ListView>(R.id.listview1)

    val items = ArrayList<String>()
    items.add("Test1")
    items.add("Test2")
    items.add("Test3")

    listview1.adapter = Adapter1(this, items)

    val transaction1 = supportFragmentManager.beginTransaction()
    fragment1Button1.setOnClickListener{

        transaction1.replace(R.id.fragment1Platz,Fragment1())
        transaction1.commit()
    }

}

}

And here is the new error message: 2020-02-02 11:15:15.935 12764-12764/strauss2002.yannick.fragmentetest E/AndroidRuntime: FATAL EXCEPTION: main Process: straus s2002.yannick.fragmentetest, PID: 12764 java.lang.RuntimeException: Unable to start activity ComponentInfo{strauss2002.yannick.fragmentetest/strauss2002.yannick.fragmentetest.MainActivity}: java.lang.IllegalStateException: listview1 must not be null at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.IllegalStateException: listview1 must not be null at strauss2002.yannick.fragmentetest.MainActivity.onCreate(MainActivity.kt:28) at android.app.Activity.performCreate(Activity.java:7009) at android.app.Activity.performCreate(Activity.java:7000) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6494)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

Here is the xml of the activity_main:

<?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=".MainActivity">

<FrameLayout
    android:id="@+id/fragment1Platz"
    android:layout_width="410dp"
    android:layout_height="495dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent">

</FrameLayout>

  <Button
    android:id="@+id/fragment1Button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="60dp"
    android:text="Öffne Fragment 1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.529"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
  </androidx.constraintlayout.widget.ConstraintLayout>

Here is the xml of the fragment_fragment1:

 <?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".Fragment1">

 <!-- TODO: Update blank fragment layout -->

 <ListView
    android:id="@+id/listview1"
    android:layout_width="match_parent"
    android:layout_height="322dp"/>

 </FrameLayout>

Here is the xml of the adapter1_layout:

 <?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:id="@+id/frameLayout"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <TextView
    android:id="@+id/adapter_textfeld1"
    android:layout_width="137dp"
    android:layout_height="53dp"
    android:layout_marginTop="128dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 </androidx.constraintlayout.widget.ConstraintLayout>
3
Please, add your a XML file with list viewMax Shwed

3 Answers

2
votes

Because you didn't call findViewById for listview1. Add listview1=findViewById(R.id.yourListViewIdInXml); just after setContentView method of oncreate

Or if you are using kotlin synthetic then make sure that listview1 resides in activity_main.xml file

Update

The problem persists because you are calling findViewById on an id(listview1) which is not available in activity_main.xml. Note that in case of activities, when you call findViewById it searches id in the layout file which is passed in setContentView method, that is activity_main.xml in your case. So when you called findViewById<ListView>(R.id.listview1) it searches listview1 in activity_main.xml, but as we know that there is no view available in activity_main.xml which has id=listview1, so findViewById simply returns null and that's why you are getting this error. So to get rid of this exception change:

val listview1=findViewById<ListView>(R.id.listview1)

to this:

val listview1=findViewById<ListView>(R.id.kontaktliste)
0
votes

I had the same issue. I resolved just putting the setContentView(activity_layout) in the onResume method.

I don't know why this error hasn't occurred in other activities with a listView

0
votes

The reason you are getting Null Exception is that you are trying to access listView1 before the fragment view is completely created which means listView1 doesn't exist yet.

So you have to move that part of the code you have in your MainActivity to the onCreateView method of your fragment like so:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val rootView = inflater.inflate(R.layout.fragment_fragment1, container, false)

        val items = ArrayList<String>()
        items.add("Test1")
        items.add("Test2")
        items.add("Test3")

        //THIS IS HOW YOU REFERENCE YOU VIEWS IN A FRAGMENT IN KOTLIN
        rootView.listview1.adapter = Adapter1(this, items)

        return rootView
    }

And pay attention that in a fragment, you have to reference the views through the inflated rootView for your fragment.