简体   繁体   中英

Android Studio: Changing MenuItem Icon at runtime

I'm trying to add an MenuItem in the Action Bar that when you press it, it changes its icon. However, at the minute when you press it, the app just crashes. I've pasted the relevant parts of the activity's java file but I can't see what I'm doing wrong. When I tested it before I added the bits about changing the icon, it worked fine so I don't think it's the sharedprefs that are making it crash. Can any one help?

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

您只需更改菜单图标imageView.setImageResource(R.drawable.ic_name)的onClickListener中的drawable即可更改图像。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM