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

折月煮酒 提交于 2019-12-13 03:07:17

问题


When I click on the first position of my list view I want it to take me to another activity, and when I click on the second position I want it to take me to a different activity..etc. I have tried to write code that when I click on the first item of my list view it takes me to the activity "MrsClubb" but whenever I click on the item it comes up with the message "unfortunately "app name" has stopped working" and then the app closes.

Any ideas?

Here is the code for various bits of my app:

MainActivity.java

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);
    }
}
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jonatboard.jonat.htssoundboard" >

<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>

</application>

</manifest>

The activity I want the item to take me to when it is click is currently empty and only has this code in it:

public class MrsClubb {
}

If you need to see any more code to help you then please let me know.


回答1:


Define the MrsClubb class as

public class MrsClubb extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState){

        setContentView(R.id.mrsClubbLayout);
        ....
        ....

    }

}

and override the onCreate() method of the Activity class (at the very least). Also add the declaration in the manifest:

<activity
    android:name=".MrsClubb"
    android:label="@string/mrs_clubb_activity_title" >
</activity>



回答2:


You need to do:

public class MrsClubb extends Activity{}

and add to your manifest:

<activity
        android:name=".MrsClubb"
        android:label="@string/title_activity_mrs_clubb" />


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

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