尝试调用虚方法' void android.widget.ListView.setAdapter(android.widget.ListAdapter)'在null对象上

时间:2016-03-09 03:36:37

标签: android listview android-adapter

我试图在listView中显示数据。我有当前代码尝试获取从myDatabase类传入的数组,然后在我的listView中显示它。

但是我收到了错误

  

"尝试调用虚方法' void android.widget.ListView.setAdapter(android.widget.ListAdapter)'在null对象引用"。

当我删除listView元素的代码时,我的代码正在主要活动中工作,我已经过测试以确保传入的数组输出正确(它确实如此)。我不确定自己错误地抛出了这个错误。

MainScreenActivity.java

public class MainScreenActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);

    // Find View-elements
    Button trackButton = (Button) findViewById(R.id.trackSelectButton);

    // Create click listener for trackSelectionButton
    trackButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {                
            trackInput();

            // Start Track List Activity
            Intent trackScreen = new Intent(v.getContext(), TrackListActivity.class);
            startActivity(trackScreen);
        }
    });        
}

public void trackInput(){

    MyDatabase mDb = new MyDatabase(this);

    String[] tracks = mDb.fetchDatabaseTracks();
    ArrayAdapter<String> trackAdapter =
            new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1,
                    android.R.id.text1, tracks);

    ListView lv = (ListView) findViewById(R.id.listView);
    lv.setAdapter(trackAdapter);
}

TrackListActivity.java

public class TrackListActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_track_list);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

}

activity_track_list.xml

<?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="com.example.autoplaylist.catalog.TrackListActivity">

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

activity_main_screen.xml

<?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" />

</RelativeLayout>

</LinearLayout>

错误日志

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.autoplaylist.catalog.MainScreenActivity.trackInput(MainScreenActivity.java:61)
at com.example.autoplaylist.catalog.MainScreenActivity$1.onClick(MainScreenActivity.java:29)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 个答案:

答案 0 :(得分:6)

您正试图找到ListView xml中没有此元素的activity_main_screen元素。因此,请将ListView添加到activity_main_screen或使用正确的xml文件。

使用

setContentView(R.layout.activity_track_list);

而不是

setContentView(R.layout.activity_main_screen);

并移动

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

onCreate()

答案 1 :(得分:0)

你在mainScreen.xml中没有ListView。 yOu实际上是将您的活动连接到mainscreen.xml。并尝试在tracklist.xml中显示您的List,这是不可能的。因此,如果您想显示列表,请执行此操作。

<?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>