App stops working when I call a second activity with nav drawer from nav drawer click

耗尽温柔 提交于 2020-02-07 03:46:26

问题


Okay so I want to have the navigation drawer available to use from all activities. I am creating a soundboard app and when I click on item 0 on the list I want it to take me to the first activity which has a navigation drawer and the first soundboard activity in it. The from that activity I want to open the nav drawer and click on another item on the list and it can take me to that activity with a soundboard in and so on... I have the nav drawer work fine when the app opens but when I click on position 0 for it to take me to another activity with the soundboard in the app closes as it has stopped working. What have I done wrong? How would I correct it to work.

This is my main activity code that opens when the app is launched:

public class MainActivity extends ActionBarActivity {

DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
String[] mDrawerListItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer);
    mDrawerList = (ListView)findViewById(android.R.id.list);
    mDrawerListItems = getResources().getStringArray(R.array.drawer_list);
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDrawerListItems));
    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch(position) {
                case 0:
                    Intent i = new Intent(MainActivity.this, MrsClubb.class);
                    startActivity(i);
            }
            mDrawerLayout.closeDrawer(mDrawerList);

        }
    });
    mDrawerToggle = new ActionBarDrawerToggle(this,
            mDrawerLayout,
            toolbar,
            R.string.drawer_open,
            R.string.drawer_close){
        public void onDrawerClosed(View v){
            super.onDrawerClosed(v);
            invalidateOptionsMenu();
            syncState();
        }
        public void onDrawerOpened(View v){
            super.onDrawerOpened(v);
            invalidateOptionsMenu();
            syncState();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    mDrawerToggle.syncState();
}

@Override
protected void onPostCreate(Bundle savedInstanceState){
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
        case android.R.id.home: {
            if (mDrawerLayout.isDrawerOpen(mDrawerList)){
                mDrawerLayout.closeDrawer(mDrawerList);
            } else {
                mDrawerLayout.openDrawer(mDrawerList);
            }
            return true;
        }
        default: return super.onOptionsItemSelected(item);
    }
}
}

This is my second class that I wish the position 0 on the list view to take me to with the soundboard in:

public class MrsClubb extends ActionBarActivity {

DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
String[] mDrawerListItems;



SoundPool Clubb1;
int clubb1Id;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mrsclubb);
Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb1Id = Clubb1.load(this, R.raw.clubb1,1);


    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer);
    mDrawerList = (ListView)findViewById(android.R.id.list);
    mDrawerListItems = getResources().getStringArray(R.array.drawer_list);
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDrawerListItems));
    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch(position) {
                case 0:
                    Intent i = new Intent(MrsClubb.this, MrsClubb.class);
                    startActivity(i);
            }
            mDrawerLayout.closeDrawer(mDrawerList);

        }
    });
    mDrawerToggle = new ActionBarDrawerToggle(this,
            mDrawerLayout,
            toolbar,
            R.string.drawer_open,
            R.string.drawer_close){
        public void onDrawerClosed(View v){
            super.onDrawerClosed(v);
            invalidateOptionsMenu();
            syncState();
        }
        public void onDrawerOpened(View v){
            super.onDrawerOpened(v);
            invalidateOptionsMenu();
            syncState();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    mDrawerToggle.syncState();
}

@Override
protected void onPostCreate(Bundle savedInstanceState){
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
        case android.R.id.home: {
            if (mDrawerLayout.isDrawerOpen(mDrawerList)){
                mDrawerLayout.closeDrawer(mDrawerList);
            } else {
                mDrawerLayout.openDrawer(mDrawerList);
            }
            return true;
        }
        default: return super.onOptionsItemSelected(item);
    }
} public void playSound1(View view) {Clubb1.play(clubb1Id, 1, 1, 1, 0, 1);}

}

This is the manifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MrsClubb"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MRSCLUBB" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

Here is the LogCat:

-30 21:58:33.984 1971-1971/com.jonatboard.jonat.htssoundboard D/AndroidRuntime﹕ Shutting
down VM
 --------- beginning of crash
03-30 21:58:33.985 1971-1971/com.jonatboard.jonat.htssoundboard E/AndroidRuntime﹕
FATAL EXCEPTION: main
 Process: com.jonatboard.jonat.htssoundboard, PID: 1971
 java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.jonatboard.jonat.htssoundboard/com.jonatboard.jonat.htssoundboard.MrsClu
bb}: java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
 at android.app.ActivityThread.access$800(ActivityThread.java:144)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:135)
 at android.app.ActivityThread.main(ActivityThread.java:5221)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
 at com.jonatboard.jonat.htssoundboard.MrsClubb.onCreate(MrsClubb.java:49)
 at android.app.Activity.performCreate(Activity.java:5933)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
 at android.app.ActivityThread.access$800(ActivityThread.java:144)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:135)
 at android.app.ActivityThread.main(ActivityThread.java:5221)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

If you need any more code let me know, your help is much appreciated.


回答1:


Your problem is with referring to the ListView in the DrawerLayout incorrectly in the MrsClubb activity. Here is an example of how you should be using and referring to the ListView.

<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">
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <ListView
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"/>
</android.support.v4.widget.DrawerLayout>

Now you can refer to the ListView in the DrawerLayout as:

ListView lv = (ListView) findViewByid(R.id.drawer);

Using Android provided IDs can sometimes get tricky and I believe this is where you have gone wrong.




回答2:


Here is your problem:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

Looks like your listView is null, check to make sure you are declaring and finding it correctly in your activity.



来源:https://stackoverflow.com/questions/29355881/app-stops-working-when-i-call-a-second-activity-with-nav-drawer-from-nav-drawer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!