3

我正在尝试Navagation Drawer在我的应用程序中使用 a 并且我一直遇到 NullPointerExceptions 并且我不明白为什么。我目前主要基于此开发人员链接的代码是:

public class MainActivity extends Activity {
private String[] mPages;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;

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

    mPages = getResources().getStringArray(R.array.page_titles);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mPages));

    mTitle = mDrawerTitle = getTitle();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, 
    mDrawerLayout,
    R.drawable.ic_drawer, 
    R.string.drawer_open, 
    R.string.drawer_close 
    ) {


        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
        }


        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
        }
    };


    mDrawerLayout.setDrawerListener(mDrawerToggle);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

和我的drawer_layout.xml:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>

我的堆栈跟踪:

08-12 15:55:54.524: E/AndroidRuntime(1409): FATAL EXCEPTION: main
08-12 15:55:54.524: E/AndroidRuntime(1409): java.lang.RuntimeException:     Unable to start activity     ComponentInfo{com.test.android.nav/com.test.android.nav.MainActivity}:     java.lang.NullPointerException
08-12 15:55:54.524: E/AndroidRuntime(1409):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.os.Looper.loop(Looper.java:137)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at java.lang.reflect.Method.invokeNative(Native Method)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at java.lang.reflect.Method.invoke(Method.java:511)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at dalvik.system.NativeStart.main(Native Method)
08-12 15:55:54.524: E/AndroidRuntime(1409): Caused by: java.lang.NullPointerException
08-12 15:55:54.524: E/AndroidRuntime(1409):     at com.test.android.nav.MainActivity.onCreate(MainActivity.java:30)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.app.Activity.performCreate(Activity.java:5008)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-12 15:55:54.524: E/AndroidRuntime(1409):     ... 11 more

第 30 行是:mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPages));我的 res 文件夹中确实有它作为 xml 文件。

现在,在我输入那条线之前,我在NullPointerException这条线上得到了一个:mDrawerLayout.setDrawerListener(mDrawerToggle);

任何帮助将非常感激!

4

1 回答 1

4

我看到您的 NullPointerException 的两个潜在原因:

第一个可能是这一行:

mPages = getResources().getStringArray(R.array.page_titles);

不向 mPages 返回有效数组。我想您的资源文件夹中没有一个名为“page_titles”的字符串数组

如果您发布的代码绝对正确,则另一个原因可能是这一行:

setContentView(R.layout.activity_main);

因为根据您的代码,navigationdrawer 的布局文件是 drawer_layout.xml。所以它应该是这样的:

setContentView(R.layout.drawer_layout);

在我看来,您的 DrawerLayout 的资源 ID 和 .xml 文件的名称都是“drawer_layout”,这非常具有误导性,我强烈建议您更改它。

此外,我看不出您调用此行的原因:

 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

在你的 onCreate() 方法中两次。

于 2013-08-12T20:08:43.247 回答