其他分享
首页 > 其他分享> > 初始化NavigationDrawer时出现NullPointerException

初始化NavigationDrawer时出现NullPointerException

作者:互联网

我试图在我的应用程序中使用导航抽屉,但我一直遇到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);

任何帮助将非常感激!

解决方法:

我看到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()方法中两次.

标签:nullpointerexception,android-navigation,android
来源: https://codeday.me/bug/20191030/1968744.html