我试图在我的应用程序中使用导航抽屉,但我一直遇到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;@OverrIDeprotected 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);}@OverrIDeprotected voID onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState();}@OverrIDepublic voID onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig);}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) { if (mDrawerToggle.onoptionsItemSelected(item)) { return true; } return super.onoptionsItemSelected(item);}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true;}}
和我的drawer_layout.xml:
<androID.support.v4.Widget.DrawerLayoutxmlns: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: main08-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.NullPointerException08-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.access0(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.NullPointerException08-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);
任何帮助将非常感激!
解决方法:
我看到NullPointerException的两个潜在原因:
第一个可能是这一行:
mPages = getResources().getStringArray(R.array.page_Titles);
不会将有效数组返回给mPages.我想您的资源文件夹中没有名为“ page_Titles”的字符串数组.
如果您发布的代码绝对正确,则另一原因可能是此行:
setContentVIEw(R.layout.activity_main);
因为根据您的代码,导航抽屉的布局文件是抽屉_布局.xml.所以它应该看起来像这样:
setContentVIEw(R.layout.drawer_layout);
我认为,您的DrawerLayout的资源ID和.xml文件的名称都为“ drawer_layout”,这非常容易引起误解,我强烈建议您进行更改.
此外,我看不出您为什么要拨打此行的原因:
mDrawerLayout = (DrawerLayout) findVIEwByID(R.ID.drawer_layout);
在您的onCreate()方法中两次.
总结以上是内存溢出为你收集整理的初始化NavigationDrawer时出现NullPointerException全部内容,希望文章能够帮你解决初始化NavigationDrawer时出现NullPointerException所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)