4
アクションプロバイダでメニューを作成すると、ClassCastException
エラーが発生し、クラスをView
にキャストできないというエラーが表示されます。私はDevGuideのActionProviders
の小さなチュートリアルを使って何が間違っているのか分かりません。ClassCastExceptionでカスタムアクションプロバイダ
public class CustomActionView extends ActionProvider{
private final Context mContext;
public CustomActionView(Context context) {
super(context);
mContext = context;
}
@Override
public View onCreateActionView() {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.action_layout,null);
ImageButton button = (ImageButton)view.findViewById(R.id.imageButton1);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent i = new Intent(mContext,com.app.notifyme.SmsPrefs.class);
mContext.startActivity(i);
}
});
return view;
}
@Override
public boolean onPerformDefaultAction() {
Intent i = new Intent(mContext,com.app.notifyme.SmsPrefs.class);
mContext.startActivity(i);
return true;
}
}
メニューXML
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/prefs"
android:title="Home"
android:icon="@drawable/android"
android:actionViewClass="com.app.notifyme.ICS.CustomActionView"/>
<item android:id="@+id/add"
android:title="SMS/MMS"
android:icon="@drawable/android"
android:actionViewClass="com.app.notifyme.ICS.ContactActionView"/>
</menu>
logcatエラー:
12-21 18:28:58.358: E/AndroidRuntime(17607): java.lang.ClassCastException: com.app.notifyme.ICS.CustomActionView cannot be cast to android.view.View
12-21 18:28:58.358: E/AndroidRuntime(17607): at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:415)
12-21 18:28:58.358: E/AndroidRuntime(17607): at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:436)
12-21 18:28:58.358: E/AndroidRuntime(17607): at android.view.MenuInflater.parseMenu(MenuInflater.java:173)
12-21 18:28:58.358: E/AndroidRuntime(17607): at android.view.MenuInflater.inflate(MenuInflater.java:95)
12-21 18:28:58.358: E/AndroidRuntime(17607): at com.app.notifyme.SmsMain.onCreateOptionsMenu(SmsMain.java:68)
12-21 18:28:58.358: E/AndroidRuntime(17607): at android.app.Activity.onCreatePanelMenu(Activity.java:2444)
12-21 18:28:58.358: E/AndroidRuntime(17607): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:388)
12-21 18:28:58.358: E/AndroidRuntime(17607): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:739)
12-21 18:28:58.358: E/AndroidRuntime(17607): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2811)
12-21 18:28:58.358: E/AndroidRuntime(17607): at android.os.Handler.handleCallback(Handler.java:605)
12-21 18:28:58.358: E/AndroidRuntime(17607): at android.os.Handler.dispatchMessage(Handler.java:92)
12-21 18:28:58.358: E/AndroidRuntime(17607): at android.os.Looper.loop(Looper.java:137)
12-21 18:28:58.358: E/AndroidRuntime(17607): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-21 18:28:58.358: E/AndroidRuntime(17607): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 18:28:58.358: E/AndroidRuntime(17607): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 18:28:58.358: E/AndroidRuntime(17607): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-21 18:28:58.358: E/AndroidRuntime(17607): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-21 18:28:58.358: E/AndroidRuntime(17607): at dalvik.system.NativeStart.main(Native Method)