我目前正在为菜单创建一个 onclick 事件并得到一个 ClassCastException 并且我不确定出了什么问题,
菜单创建者和 onclick 事件
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public void addFunction(){
Intent intent = new Intent(timetable.this,AddEntry.class );
startActivity(intent);
}
public void deleteFunction(){
Intent intent = new Intent(timetable.this,DeleteEntry.class );
startActivity(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.add:
addFunction();
return true;
case R.id.delete:
deleteFunction();
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.paad.timetable"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".timetable"
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="com.paad.timetable.AddEntry"
android:label="@string/add">
<intent-filter>
<action android:name="android.intent.AddEntry" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
和菜单的 XML 布局
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/add"
android:icon="@drawable/add"
android:title="@string/add"/>
<item android:id="@+id/delete"
android:icon="@drawable/delete"
android:title="@string/delete"
/>
</menu>
它可能是我想念的小东西,但任何帮助将不胜感激:)
编辑:日志
05-01 06:31:26.781: ERROR/AndroidRuntime(408): FATAL EXCEPTION: main
05-01 06:31:26.781: ERROR/AndroidRuntime(408): java.lang.RuntimeException: Unable to
instantiate activity ComponentInfo
{com.paad.timetable/com.paad.timetable.AddEntry}:
java.lang.ClassCastException: com.paad.timetable.AddEntry
05-01 06:31:26.781: ERROR/AndroidRuntime(408): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-01 06:31:26.781: ERROR/AndroidRuntime(408): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-01 06:31:26.781: ERROR/AndroidRuntime(408): at
android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-01 06:31:26.781: ERROR/AndroidRuntime(408): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-01 06:31:26.781: ERROR/AndroidRuntime(408): at
android.os.Handler.dispatchMessage(Handler.java:99)
05-01 06:31:26.781: ERROR/AndroidRuntime(408): at android.os.Looper.loop
(Looper.java:123)
05-01 06:31:26.781: ERROR/AndroidRuntime(408): at android.app.ActivityThread.main
(ActivityThread.java:4627)
05-01 06:31:26.781: ERROR/AndroidRuntime(408): at
java.lang.reflect.Method.invokeNative(Native Method)
05-01 06:31:26.781: ERROR/AndroidRuntime(408): at java.lang.reflect.Method.invoke
(Method.java:521)
05-01 06:31:26.781: ERROR/AndroidRuntime(408): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-01 06:31:26.781: ERROR/AndroidRuntime(408): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-01 06:31:26.781: ERROR/AndroidRuntime(408): at dalvik.system.NativeStart.main
(Native Method)
05-01 06:31:26.781: ERROR/AndroidRuntime(408): Caused by:
java.lang.ClassCastException: com.paad.timetable.AddEntry
05-01 06:31:26.781: ERROR/AndroidRuntime(408): at
android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-01 06:31:26.781: ERROR/AndroidRuntime(408): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-01 06:31:26.781: ERROR/AndroidRuntime(408): ... 11 more