0

我正在尝试设置数组适配器并有以下代码:

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

我试图设置适配器的最后一行代码,我得到一个 NullPointerException。如果我尝试导航到包含此代码的页面,应用程序会崩溃。

任何指针?

这是调试:

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)

4

2 回答 2

0

在 ArrayAdapter 的构造函数中,您必须按照 Android 文档中的说明传递 TextView id: ArrayAdapter

互联网上有一些关于这个主题的教程,例如这个:ArrayAdapter basic tutorial

你的问题也可能来自mServiceswhich 可以为空。


编辑:

以下代码适用于我:

活动:

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

activity_main.xml 文件:

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

和 text_view.xml 文件:

<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>
于 2013-07-08T15:21:33.820 回答
0

你试过这条线吗?

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

如果这比 mServices 数组更能解决您的问题..

于 2013-07-08T15:33:01.547 回答