我正在尝试在我的应用程序中使用Navagation Drawer
并继续运行NullPointerExceptions,我不明白为什么。我目前基于this developer link的代码是:
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);
非常感谢任何帮助!
答案 0 :(得分: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()方法中两次。