简体   繁体   中英

Android null pointer exception in ListView

I've got a simple ListView and an ArrayAdapter. But when android goes to draw the screen, I get a null pointer exception every time. Here's the stack:

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]  

The code is really simple too. The 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>

And the code:

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

I've seen tons of examples that do this same thing, and most people are getting this error when they have a string array with some null pointers in it. As you can see, this is not the case here. Anyone else fought with this before?

Following is the official java doc for the ArrayAdapter you are using:

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

Added in API level 1 Constructor

context The current context.

resource The resource ID for a layout file containing a layout to use when instantiating views.

textViewResourceId The id of the TextView within the layout resource to be populated

objects The objects to represent in the ListView.

So the second parameter should be the layout file containing the view for you row item in list mind you not the list. the layout should be for one row in the list...

and the third value should be the id of the text view within the layout given as second parameter where list view can insert the value stored in the string array.

you should not use "this" just like the way you are using, instead use ActivityName.this .. means you should write activity name before "this"

if your activity is

ActivityName extends Activity {

}

then

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM