如何修复由调用简单适配器引起的null异常,它使我的应用程序崩溃:/

时间:2019-05-04 10:13:09

标签: java android simpleadapter

我正在列表视图。当我打开列表视图时,应用程序崩溃。

SimpleAdapter  simpleAdapter = new SimpleAdapter(this, wordData,android.R.layout.simple_list_item_2, new String[] {"content","username"}, new int[] {android.R.id.text1,android.R.id.text2});
    line 37 :        listView.setAdapter(simpleAdapter); // crashes the app//

logcat:-
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.spreadword/com.example.spreadword.FeedActivity}: 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:2841)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2919)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1637)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at com.example.spreadword.FeedActivity.onCreate(FeedActivity.java:37)
        at android.app.Activity.performCreate(Activity.java:7074)
        at android.app.Activity.performCreate(Activity.java:7065)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2794)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2919) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1637) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:176) 
        at android.app.ActivityThread.main(ActivityThread.java:6656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 

0 个答案:

没有答案