当我从导航抽屉点击调用第二个 activity 时,应用程序停止工作
App stops working when I call a second activity from nav drawer click
当我单击列表视图的第一个位置时,我希望它将我带到另一个 activity,而当我单击第二个位置时,我希望它把我带到另一个 [=31] =]..等等我尝试编写代码,当我单击列表视图的第一项时,它会将我带到 activity "MrsClubb",但每当我单击该项目时,它都会显示消息 "unfortunately "应用程序名称“已停止工作”,然后应用程序关闭。
有什么想法吗?
这是我的应用程序各个部分的代码:
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);
}
}
}
清单:
<?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>
activity 我希望该项目在单击时带我到当前是空的,其中只有此代码:
public class MrsClubb {
}
如果您需要查看更多代码来帮助您,请告诉我。
定义 MrsClubb
class 为
public class MrsClubb extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
setContentView(R.id.mrsClubbLayout);
....
....
}
}
并覆盖 Activity
class 的 onCreate()
方法(至少)。还要在清单中添加声明:
<activity
android:name=".MrsClubb"
android:label="@string/mrs_clubb_activity_title" >
</activity>
您需要做的:
public class MrsClubb extends Activity{}
并添加到您的清单中:
<activity
android:name=".MrsClubb"
android:label="@string/title_activity_mrs_clubb" />
当我单击列表视图的第一个位置时,我希望它将我带到另一个 activity,而当我单击第二个位置时,我希望它把我带到另一个 [=31] =]..等等我尝试编写代码,当我单击列表视图的第一项时,它会将我带到 activity "MrsClubb",但每当我单击该项目时,它都会显示消息 "unfortunately "应用程序名称“已停止工作”,然后应用程序关闭。
有什么想法吗?
这是我的应用程序各个部分的代码:
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);
}
}
}
清单:
<?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>
activity 我希望该项目在单击时带我到当前是空的,其中只有此代码:
public class MrsClubb {
}
如果您需要查看更多代码来帮助您,请告诉我。
定义 MrsClubb
class 为
public class MrsClubb extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
setContentView(R.id.mrsClubbLayout);
....
....
}
}
并覆盖 Activity
class 的 onCreate()
方法(至少)。还要在清单中添加声明:
<activity
android:name=".MrsClubb"
android:label="@string/mrs_clubb_activity_title" >
</activity>
您需要做的:
public class MrsClubb extends Activity{}
并添加到您的清单中:
<activity
android:name=".MrsClubb"
android:label="@string/title_activity_mrs_clubb" />