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

Android Firebase listView应用程序在填充错误时崩溃:java.lang.NullPointerException,android,listview,firebase,android-recyclerview,adapter,Android,Listview,Firebase,Android Recyclerview,Adapter,填充ListView应用程序时崩溃 错误:空点异常 public class Recycler extends AppCompatActivity { private ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.la

填充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);
    }
}

对于FirebaseListAdapter,您使用android.R.layout.simple\u list\u item\u 1作为每个列表项的布局

populateView方法中,您正在查找视图android.R.id.listpopulateView方法是在布局视图中指定值的方法,以下是布局中的视图,android.R.layout.simple_list_item_1

致:

使用android.R指的是android SDK内置的资源,R指的是您在应用程序中提供或创建的资源

有关更多信息,请参阅本文:

更改代码

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


好吧,我也这么做了。ps:ListView的Im new hereid是仅列表受保护的void populateView(视图v,字符串模型,int位置){ListView text=(ListView)findViewById(R.id.list);text.text(模型);}用于列表适配器,您正在使用android.R.layout.simple_list_item_1作为列表项的布局,android.R.layout.simple_list_item_1将android.R.id.text1作为值的容器。您的R.id.list将引用您的整个列表,而不是用于放置在列表中的值的容器
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) 
<?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"/>
mListView = (ListView) findViewById(android.R.id.list);
mListView = (ListView) findViewById(R.id.list);
           TextView text = (TextView) findViewById(android.R.id.list);
         TextView textView = (TextView) v.findViewById(android.R.id.text1);