NullPointerException with setAdapter - Trying to set array adapter

北战南征 提交于 2019-12-11 14:15:28

问题


I am trying to set array adapter and have following code in place:

private String[] mServices;
private ListView mDrawerList;
//Ends Here

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //For Navigation Drawer
    mServices = getResources().getStringArray(R.array.drawer_services);
    mDrawerList = (ListView) findViewById (R.id.left_drawer);

    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mServices));

The last line of code where I am trying to set Adapter, I get a NullPointerException. The app crashes if I try to navigate to page containing this code.

Any pointers?

Here's the debug:

07-08 20:05:22.885: E/AndroidRuntime(2351): FATAL EXCEPTION: main 07-08 20:05:22.885: E/AndroidRuntime(2351): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.parrot.urlripper/com.parrot.urlripper.google}: java.lang.NullPointerException 07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 07-08 20:05:22.885: E/AndroidRuntime(2351): at android.os.Handler.dispatchMessage(Handler.java:99) 07-08 20:05:22.885: E/AndroidRuntime(2351): at android.os.Looper.loop(Looper.java:123) 07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.main(ActivityThread.java:3683) 07-08 20:05:22.885: E/AndroidRuntime(2351): at java.lang.reflect.Method.invokeNative(Native Method) 07-08 20:05:22.885: E/AndroidRuntime(2351): at java.lang.reflect.Method.invoke(Method.java:507) 07-08 20:05:22.885: E/AndroidRuntime(2351): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 07-08 20:05:22.885: E/AndroidRuntime(2351): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 07-08 20:05:22.885: E/AndroidRuntime(2351): at dalvik.system.NativeStart.main(Native Method) 07-08 20:05:22.885: E/AndroidRuntime(2351): Caused by: java.lang.NullPointerException 07-08 20:05:22.885: E/AndroidRuntime(2351): at com.parrot.urlripper.google.onCreate(google.java:35) 07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)


回答1:


In your constructor of ArrayAdapter, you have to pass a TextView id as described in the Android documentation : ArrayAdapter

There is some tutorial on internet about this subject, for example this one : ArrayAdapter basic tutorial

And your problem could also come from the mServices which can be null.


EDIT:

The following code works for me:

The Activity:

public class MainActivity extends Activity {

    private String[] mServices = new String[] {"Test1", "Test2", "Test3", "Test4"};
    private ListView mDrawerList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.text_view, mServices));
    }
}

The activity_main.xml file :

<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:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false"
        android:choiceMode="multipleChoice"
    />
</LinearLayout>

And the text_view.xml file:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    android:textColor="@android:color/black"
    android:textStyle="italic">
</TextView>



回答2:


have you tried this line .

mDrawerList.setAdapter(new ArrayAdapter(this, R.layout.drawer_list_item, {"test","test","test"}));

if this solves your problem than ,than mServices array is the problem ..



来源:https://stackoverflow.com/questions/17529418/nullpointerexception-with-setadapter-trying-to-set-array-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!