1

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

1 回答 1

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 有问题。

于 2012-05-01T06:49:32.227 回答