菜单onOptionsItemSelected给出ClassCastException

时间:2012-05-01 06:40:23

标签: android menu onclick

我当前正在为菜单创建一个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

1 个答案:

答案 0 :(得分:0)

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.DatePicker;
import android.widget.TimePicker;


public class ScheduleActivity extends Activity {

SQLiteDatabase db;

TimePicker timePicker;

DatePicker datePicker;

int hour, minute;

int yr, month, day;

static String fDate = "DepartureDate";

static String fTime = "DEPART_TIME";


/** Called when the activity is first created */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.schedule_layout);

}


public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}


public void addFunction() {

    Log.i("Add Function", "True");
}


public void deleteFunction() {

    Log.i("Delete Function", "True");
}


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

此代码工作正常,AddEntry / deleteEntry Activity必须存在问题。