listview适配器中的Nullpointer异常

时间:2012-03-14 05:23:39

标签: android android-listview adapter

我正在使用arrayadapter在listview上显示数据。我编码的内容如下所示bt我得到空指针异常。请告诉我我错过了什么?

ArrayAdapter<String> adapter;
setContentView(R.layout.menu_home_activity);
        OptionsList = (ListView) findViewById(R.id.OptionsLists);
String[] array = getResources().getStringArray(R.array.personal_settings);
            adapter = new ArrayAdapter<String>(MenuHomeActivity.this,
                    R.layout.menu_home_activity, R.id.text, array);
            OptionsList.setAdapter(adapter);

menu_home_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:name="@+id/OptionsLists"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

Logcat说:

03-14 10:54:59.242: E/AndroidRuntime(267): Uncaught handler: thread main exiting due to uncaught exception
03-14 10:54:59.252: E/AndroidRuntime(267): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.biz.elife/com.biz.elife.MenuHomeActivity}: java.lang.NullPointerException
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.os.Looper.loop(Looper.java:123)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.app.ActivityThread.main(ActivityThread.java:4363)
03-14 10:54:59.252: E/AndroidRuntime(267):  at java.lang.reflect.Method.invokeNative(Native Method)
03-14 10:54:59.252: E/AndroidRuntime(267):  at java.lang.reflect.Method.invoke(Method.java:521)
03-14 10:54:59.252: E/AndroidRuntime(267):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-14 10:54:59.252: E/AndroidRuntime(267):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-14 10:54:59.252: E/AndroidRuntime(267):  at dalvik.system.NativeStart.main(Native Method)
03-14 10:54:59.252: E/AndroidRuntime(267): Caused by: java.lang.NullPointerException
03-14 10:54:59.252: E/AndroidRuntime(267):  at com.biz.elife.MenuHomeActivity.onCreate(MenuHomeActivity.java:33)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-14 10:54:59.252: E/AndroidRuntime(267):  ... 11 more

修改:

第33行是

OptionsList.setAdapter(adapter);

当我尝试将适配器设置为列表时,如果我删除此行没有显示错误,并且屏幕上没有显示任何组件,只显示黑屏,则会出现错误

感谢大家宝贵的时间和回复我的错误是在menu_home_activity我添加了android:name="@+id/OptionsList" 我改为

android:id="@+id/OptionsList"

5 个答案:

答案 0 :(得分:1)

我认为以下代码可以帮助您解决问题:

public class ArrayAdapterDemo extends ListActivity {
   TextView selection;
   String[] items = { "this", "is", "a", "really", 
   "silly", "list" };
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(
      this,
      android.R.layout.simple_expandable_list_item_1,
      items));
selection=(TextView)findViewById(R.id.selection); 
   }

答案 1 :(得分:1)

尝试将代码放在try-catch块中 我想

OptionsList = (ListView) findViewById(R.id.OptionsLists);

导致空指针。更好地处理可能的异常代码(null或其他)。

答案 2 :(得分:1)

只需替换此

      adapter = new ArrayAdapter<String>(MenuHomeActivity.this,
                R.layout.menu_home_activity, R.id.text, array);

代码中的

   adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array);

答案 3 :(得分:0)

问题出在相关活动的java文件的第33行。我猜你的列表视图没有正确初始化。

答案 4 :(得分:0)

你的optionsList没有正确创建,这可能是因为ure xml文件存在一些问题。 ListView没有正确充气;

尝试用android中的fill_parent替换match_parent:layout_width =“match_parent”和android:layout_height =“match_parent”

希望这有帮助。