我正在尝试在操作栏中添加一个MenuItem,当您按下它时,它会更改其图标。但是,在你按下它的那一刻,应用程序就崩溃了。我已经粘贴了活动的java文件的相关部分,但我看不出我做错了什么。当我在添加关于更改图标的位之前测试它时,它工作正常,所以我不认为这是使它崩溃的共享优化。任何人都可以帮忙吗?
@Override
public boolean onCreateOptionsMenu (Menu menu){
getMenuInflater().inflate(R.menu.parktool, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
MenuItem star = (MenuItem)findViewById(R.id.action_fav);
int id = item.getItemId();
SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("Alton", Context.MODE_PRIVATE);
if (id == R.id.action_fav){
fav = sharedPref.getInt("Fav", 0);
if(fav == 0){
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("Fav", (int) 1);
editor.commit();
star.setIcon(R.drawable.favorite2);
Toast toast = Toast.makeText(getApplicationContext(), "Alton Towers added to favorite parks.", Toast.LENGTH_SHORT);
toast.show();
}
else if (fav == 1){
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("Fav", (int) 0);
editor.commit();
star.setIcon(R.drawable.favorite1);
Toast toast = Toast.makeText(getApplicationContext(), "Alton Towers removed to favorite parks.", Toast.LENGTH_SHORT);
toast.show();
}
}
_
01-28 14:55:54.081 14763-14763/com.coastercounter.nyphoria.coastercounter E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.coastercounter.nyphoria.coastercounter, PID: 14763
java.lang.ClassCastException: android.support.v7.view.menu.ActionMenuItemView cannot be cast to android.view.MenuItem
at com.coastercounter.nyphoria.coastercounter.AltonTowers.onOptionsItemSelected(AltonTowers.java:97)
at android.app.Activity.onMenuItemSelected(Activity.java:3204)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:421)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:620)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
at android.view.View.performClick(View.java:5702)
at android.widget.TextView.performClick(TextView.java:10888)
at android.view.View$PerformClick.run(View.java:22541)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:0)
您可以通过简单地更改菜单图标imageView.setImageResource(R.drawable.ic_name。
的onClickListener中的drawable来更改图像。