简体   繁体   English

Android Studio:在运行时更改MenuItem图标

[英]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. 我试图在操作栏中添加一个MenuItem,当您按下它时,它会更改其图标。 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. 我已经粘贴了活动的Java文件的相关部分,但看不到我做错了什么。 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. 当我在添加有关更改图标的内容之前对其进行测试时,它工作正常,因此我不认为是导致它崩溃的sharedprefs。 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即可更改图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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