尝试在空对象ref上调用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' [英] Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object ref

查看:73
本文介绍了尝试在空对象ref上调用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to display data in a listView. I have the current code attempting to take an array that is passed in from the myDatabase class, and then display this within my listView.

但是我遇到了错误:

试图在空对象引用上调用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'".

"Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference".

当我删除listView元素的代码时,我的代码正在主要活动中工作,在该位置,我已经测试过以确保正确传入传入的数组输出(确实如此).我不确定要怎么做才能引发此错误.

My code is working in main activity when I remove the code for the listView element, in it's place I've tested to ensure the passed in array outputs correctly (which it does). I'm not sure what I'm doing wrong to have this error thrown up.

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)

推荐答案

您正试图在activity_main_screen xml中找到没有此类元素的ListView元素.因此,将ListView添加到您的activity_main_screen或使用适当的xml文件.

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.

使用

setContentView(R.layout.activity_track_list);

代替

setContentView(R.layout.activity_main_screen);

然后移动

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

onCreate()

这篇关于尝试在空对象ref上调用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆