Firebase listView应用程序崩溃时填充错误:java.lang.NullPointerException

时间:2017-03-27 00:27:45

标签: android listview firebase android-recyclerview adapter

填充ListView应用程序时崩溃。

错误:null point Exception

public class Recycler extends AppCompatActivity
{
    private ListView mListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);

        mListView = (ListView) findViewById(android.R.id.list);

        DatabaseReference ref = FirebaseDatabase.getInstance().getReferenceFromUrl("https://afirebaseproject-eab4d.firebaseio.com/data/a");

        FirebaseListAdapter<String> adapter = new FirebaseListAdapter<String>(
                this,
                String.class,
                android.R.layout.simple_list_item_1,
                ref
        )
        {
            @Override
            protected void populateView(View v, String model, int position) {

               TextView text = (TextView) findViewById(android.R.id.list);

                text.setText(model);
            }
        };
        mListView.setAdapter(adapter);
    }
}

XML ListView id- list

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    ....
    tools:context="shah.parth.temp2.Recycler">

    <ListView

        android:id="@+id/list"
        style="@android:style/Widget.ListView" />
</android.support.constraint.ConstraintLayout>

这是我得到的错误

FATAL EXCEPTION: main
    Process: shah.parth.temp2, PID: 2845
    java.lang.RuntimeException: Unable to start activity ComponentInfo{shah.parth.temp2/shah.parth.temp2.Recycler}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
        at android.app.ActivityThread.access$900(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5441)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at shah.parth.temp2.Recycler.onCreate(Recycler.java:41)
        at android.app.Activity.performCreate(Activity.java:6303)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483) 
        at android.app.ActivityThread.access$900(ActivityThread.java:153) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5441) 
        at java.lang.reflect.Method.invoke(Native Method) 

2 个答案:

答案 0 :(得分:0)

对于您的FirebaseListAdapter,您使用 android.R.layout.simple_list_item_1 作为每个ListItem的布局。

populateView 方法中,您正在寻找视图 android.R.id.list populateView 方法是您在布局视图中分配值的方法,以及布局中的视图 android.R.layout.simple_list_item_1 https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/simple_list_item_1.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@android:id/text1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:textAppearance="?android:attr/textAppearanceListItemSmall"
   android:gravity="center_vertical"
   android:paddingStart="?android:attr/listPreferredItemPaddingStart"
   android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
   android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

如果您查看代码,则没有ID为列表的视图,而是ID为 text1 的视图。有了这个,当findViewById方法开始查找ID为 list 的视图时,它什么都没有,当你 setText 为空时,它会调用的NullPointerException

要解决此问题,只需将 populateView 方法中的ID更改为 text1 ,即可获得相应的视图。

您还应该在第12行更改代码:

mListView = (ListView) findViewById(android.R.id.list);

为:

mListView = (ListView) findViewById(R.id.list);

使用 android.R 会引用Android SDK内置的资源, R 会引用您在应用中提供或创建的资源。

有关详细信息,请参阅此帖子:Difference between R.layout and android.R.layout

答案 1 :(得分:0)

更改您的代码

           TextView text = (TextView) findViewById(android.R.id.list);

         TextView textView = (TextView) v.findViewById(android.R.id.text1);