我刚刚创建了一个滑动菜单但是当我按下菜单中的项目时,他们没有打开我设置的活动。这是onItemClick方法的代码:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Highlight the selected item, update the title, and close the drawer
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
//You should reset item counter
mDrawer.closeDrawer(mDrawerList);
if ( position == 1 ) {
Intent intent = new Intent(MainActivity.this, PrimActivitate.class);
startActivity(intent);
mDrawer.closeDrawers();
if ( position == 2 ) {
Intent intent2 = new Intent(MainActivity.this, Profesori.class);
startActivity(intent2);
mDrawer.closeDrawers();
}
问题是:当我按下第一个项目时,它会崩溃应用程序,当我按下第二个项目时它没有做任何事情......
编辑:
02-20 15:27:43.991: E/AndroidRuntime(1278): FATAL EXCEPTION: main
02-20 15:27:43.991: E/AndroidRuntime(1278): Process: com.orar.cngcnasaud, PID: 1278
02-20 15:27:43.991: E/AndroidRuntime(1278): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.orar.cngcnasaud/com.orar.cngcnasaud.PrimActivitate}; have you declared this activity in your AndroidManifest.xml?
02-20 15:27:43.991: E/AndroidRuntime(1278): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
02-20 15:27:43.991: E/AndroidRuntime(1278): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
02-20 15:27:43.991: E/AndroidRuntime(1278): at android.app.Activity.startActivityForResult(Activity.java:3424)
02-20 15:27:43.991: E/AndroidRuntime(1278): at android.app.Activity.startActivityForResult(Activity.java:3385)
02-20 15:27:43.991: E/AndroidRuntime(1278): at android.app.Activity.startActivity(Activity.java:3627)
02-20 15:27:43.991: E/AndroidRuntime(1278): at android.app.Activity.startActivity(Activity.java:3595)
02-20 15:27:43.991: E/AndroidRuntime(1278): at com.orar.cngcnasaud.MainActivity$DrawerItemClickListener.onItemClick(MainActivity.java:179)
02-20 15:27:43.991: E/AndroidRuntime(1278): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
02-20 15:27:43.991: E/AndroidRuntime(1278): at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
02-20 15:27:43.991: E/AndroidRuntime(1278): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
02-20 15:27:43.991: E/AndroidRuntime(1278): at android.widget.AbsListView$3.run(AbsListView.java:3638)
02-20 15:27:43.991: E/AndroidRuntime(1278): at android.os.Handler.handleCallback(Handler.java:733)
02-20 15:27:43.991: E/AndroidRuntime(1278): at android.os.Handler.dispatchMessage(Handler.java:95)
02-20 15:27:43.991: E/AndroidRuntime(1278): at android.os.Looper.loop(Looper.java:136)
02-20 15:27:43.991: E/AndroidRuntime(1278): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-20 15:27:43.991: E/AndroidRuntime(1278): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 15:27:43.991: E/AndroidRuntime(1278): at java.lang.reflect.Method.invoke(Method.java:515)
02-20 15:27:43.991: E/AndroidRuntime(1278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-20 15:27:43.991: E/AndroidRuntime(1278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-20 15:27:43.991: E/AndroidRuntime(1278): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
当你按第二项onItemClick()时什么也没做,因为 if(position == 2) 在里面 if(position == 1)。
你也可以发布崩溃堆栈跟踪吗?
答案 1 :(得分:1)
我永远不会去位置== 2,如果(位置== 1)之前关闭if(位置== 1),请尝试:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mDrawerList.setItemChecked(position, true);
mDrawer.closeDrawer(mDrawerList);
if ( position == 1 ) {
Intent intent = new Intent(MainActivity.this, PrimActivitate.class);
MainActivity.this.startActivity(intent);
mDrawer.closeDrawers();
}
if ( position == 2 ) {
Intent intent2 = new Intent(MainActivity.this, Profesori.class);
MainActivity.this.startActivity(intent2);
mDrawer.closeDrawers();
}
}
关于您忘记添加到AndroidManifest.xml
的崩溃:
<activity android:label="@string/app_name" android:name="com.orar.cngcnasaud.PrimActivitate"/>
<activity android:label="@string/app_name" android:name="com.orar.cngcnasaud.Profesori"/>
答案 2 :(得分:1)
正如您的logcat建议的那样,您尚未在清单中声明您的PrimActivitate。将这些添加到您的AndroidManifest:
<activity android:name=".PrimActivitate"></activity>
如果不存在,则添加第二个:
<activity android:name=".Profesori"></activity>
接下来,首先移动您的第二个条件OUTSIDE,以便处理您的案件:
if (position == 1) {
Intent intent = new Intent(MainActivity.this, PrimActivitate.class);
startActivity(intent);
mDrawer.closeDrawers();
}
else if (position == 2) {
Intent intent2 = new Intent(MainActivity.this, Profesori.class);
startActivity(intent2);
mDrawer.closeDrawers();
}