Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object ref [duplicate]

删除回忆录丶 提交于 2019-12-06 10:39:47

You are trying to find the ListView element in your activity_main_screen xml where there is no such element. So, either add the ListView to your activity_main_screen or use the proper xml file.

Use

setContentView(R.layout.activity_track_list);

instead of

setContentView(R.layout.activity_main_screen);

and move

ListView lv = (ListView) findViewById(R.id.listView);

to onCreate()

You dont have a ListView in mainScreen.xml. yOu are actually wiring up your activity to mainscreen.xml. and trying to display your List in tracklist.xml which is not possible. So if you want to display your list then do this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainScreenActivity">

<TextView
    android:id="@+id/welcomeMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/opening_message"
    android:textSize="18sp"
    android:fontFamily="sans-serif-light"
    android:layout_marginBottom="10dp"/>

<RelativeLayout
    android:id="@+id/relLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/trackSelectButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="Track selection" />

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</RelativeLayout>

</LinearLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!