ListView中的Android空指针异常

时间:2013-01-04 19:05:40

标签: android listview nullpointerexception android-arrayadapter

我有一个简单的ListView和一个ArrayAdapter。但是当android去绘制屏幕时,我每次都会得到一个空指针异常。这是堆栈:

Thread [<1> main] (Suspended (exception NullPointerException))  
    <VM does not provide monitor information>   
    ArrayAdapter.createViewFromResource(int, View, ViewGroup, int) line: 392    
    ArrayAdapter.getView(int, View, ViewGroup) line: 362    
    ListView(AbsListView).obtainView(int, boolean[]) line: 2210 
    ListView.measureHeightOfChildren(int, int, int, int, int) line: 1244    
    ListView.onMeasure(int, int) line: 1155 
    ListView(View).measure(int, int) line: 12775    
    LinearLayout(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 4709    
    LinearLayout.measureChildBeforeLayout(View, int, int, int, int, int) line: 1385 
    LinearLayout.measureVertical(int, int) line: 670    
    LinearLayout.onMeasure(int, int) line: 563  
    LinearLayout(View).measure(int, int) line: 12775    
    FrameLayout(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 4709 
    FrameLayout.onMeasure(int, int) line: 293   
    FrameLayout(View).measure(int, int) line: 12775 
    LinearLayout(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 4709    
    LinearLayout.measureChildBeforeLayout(View, int, int, int, int, int) line: 1385 
    LinearLayout.measureVertical(int, int) line: 670    
    LinearLayout.onMeasure(int, int) line: 563  
    LinearLayout(View).measure(int, int) line: 12775    
    PhoneWindow$DecorView(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 4709   
    PhoneWindow$DecorView(FrameLayout).onMeasure(int, int) line: 293    
    PhoneWindow$DecorView.onMeasure(int, int) line: 2240    
    PhoneWindow$DecorView(View).measure(int, int) line: 12775   
    ViewRootImpl.performTraversals() line: 1117 
    ViewRootImpl.handleMessage(Message) line: 2505  
    ViewRootImpl(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 137 
    ActivityThread.main(String[]) line: 4514    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 511  
    ZygoteInit$MethodAndArgsCaller.run() line: 790  
    ZygoteInit.main(String[]) line: 557 
    NativeStart.main(String[]) line: not available [native method]  

代码也非常简单。 XML:

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099cc"
    tools:context=".FullscreenActivity"
    android:orientation="vertical">

    <ListView
        android:id="@+id/status_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

代码:

    String test[] = {"Line 1","Line 2"};

    ListView status = (ListView) findViewById(R.id.status_view);
    ArrayAdapter<String> statusList = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,R.id.status_view,test);     
    status.setAdapter(statusList);

我已经看到大量的例子做同样的事情,大多数人在他们有一个带有一些空指针的字符串数组时会收到这个错误。如您所见,这不是这种情况。其他人之前还打过这个吗?

2 个答案:

答案 0 :(得分:1)

以下是您正在使用的ArrayAdapter的官方java文档:

  

public ArrayAdapter(Context context,int resource,int   textViewResourceId,T [] objects)

     

在API级别1构造函数

中添加      

上下文当前上下文。

     

资源包含要使用的布局的布局文件的资源ID   在实例化视图时。

     

textViewResourceId 布局资源中TextView的ID   待填充

     

对象要在ListView中表示的对象。

所以第二个参数应该是包含列表中行项目视图的布局文件,请注意您不是列表。布局应该是列表中的一行......

并且第三个值应该是作为第二个参数给出的布局中的文本视图的id,其中列表视图可以插入存储在字符串数组中的值。

答案 1 :(得分:0)

你不应该像你使用的那样使用“this”,而是使用ActivityName.this ..意味着你应该在“this”之前写下活动名称

如果您的活动是

ActivityName extends Activity {

}

然后

String test[] = {"Line 1","Line 2"};

ListView status = (ListView) findViewById(R.id.status_view);
ArrayAdapter<String> statusList = new ArrayAdapter<String>(ActivityName.this,android.R.layout.simple_list_item_1,test);     
status.setAdapter(statusList);