简体   繁体   English

如何正确设置按钮的背景可绘制Android

[英]How to Set the Background Drawable of a Button Properly Android

I am trying to change the background of a button when the user press it in the emulator everything works fine but on a real device the app crash. 当用户在模拟器中按下按钮时,我试图更改按钮的背景,但一切正常,但在真实设备上,应用程序崩溃了。 here is my code: 这是我的代码:

answerOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               answerOne.setBackgroundResource(R.drawable.button_background_green);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                ValidateAnswer(current5,Answers_six[0]);
            }
        });

and the error in the log is: 并且日志中的错误是:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.nasrf.winners10, PID: 9715
                  android.content.res.Resources$NotFoundException: Resource ID #0x7f080060
                      at android.content.res.Resources.getValue(Resources.java:1416)
                      at android.content.res.Resources.getDrawable(Resources.java:861)
                      at android.content.Context.getDrawable(Context.java:402)
                      at android.view.View.setBackgroundResource(View.java:16423)
                      at android.support.v7.widget.AppCompatButton.setBackgroundResource(AppCompatButton.java:83)
                      at com.example.nasrf.winners10.Game.GameActivity$16.onClick(GameActivity.java:426)
                      at android.view.View.performClick(View.java:4802)
                      at android.view.View$PerformClick.run(View.java:20101)
                      at android.os.Handler.handleCallback(Handler.java:810)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:189)
                      at android.app.ActivityThread.main(ActivityThread.java:5529)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)

any help please? 有什么帮助吗?

Its happening due to the difference in the SDK of your emulator and real device. 发生这种情况的原因是模拟器和实际设备的SDK有所不同。

You can check the sdk first and then set the Background like this - 您可以先检查SDK,然后像这样设置背景-

     if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN){
         answerOne.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_background_green));
       }
       else if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1)         {
      answerOne.setBackground(getResources().getDrawable(R.drawable.button_background_green));
     } else{
       answerOne.setBackground(ContextCompat.getDrawable(this, R.drawable.button_background_green));
   }

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

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