【问题标题】:Firebase listView app crashes while populating Error: java.lang.NullPointerExceptionFirebase listView 应用程序在填充错误时崩溃:java.lang.NullPointerException
【发布时间】:2017-03-27 00:27:45
【问题描述】:

填充 ListView 应用时崩溃。

错误:空点异常

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- 列表

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

【问题讨论】:

标签: android listview firebase android-recyclerview adapter


【解决方案1】:

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

populateView 方法中,您正在寻找视图 android.R.id.listpopulateView 方法是在布局视图中分配值的方法,以下是布局中的视图 android.R.layout.simple_list_item_1https://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 为 list 的视图,而是 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 将引用您在应用程序中提供或创建的资源。 p>

查看这篇文章了解更多信息:Difference between R.layout and android.R.layout

【讨论】:

  • ListView 的 ID 只是列表
  • protected void populateView(View v, String model, int position) { ListView text = (ListView) findViewById(R.id.list);文本.文本(模型); }
  • 对于您的列表适配器,您使用 android.R.layout.simple_list_item_1 作为列表项的布局,而 android.R.layout.simple_list_item_1 使用 android.R.id.text1 作为容器价值。您的 R.id.list 将引用您的整个列表,而不是指向将放置在列表中的值的容器
【解决方案2】:

更改您的代码

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多