簡體   English   中英

.setBackgroundResource使我的應用程序崩潰

[英].setBackgroundResource crashing my app

在應用程序中,每當我嘗試檢查灰色選項並單擊我的按鈕時,我的應用程序都會崩潰。其他所有單選按鈕都可以正常工作,但是每當我嘗試通過灰色按鈕更改布局的背景色時,應用程序就會崩潰。 PS當我刪除了instructionlayout.setBackgroundResource(R.color.grey); 從代碼來看,灰色也可以正常工作,並烤“灰色”。 請幫助我,如何在我的所有布局上更改我的背景色,而不會導致應用崩潰。

public void setter(View setv)
{
    RelativeLayout instructionlayout = (RelativeLayout)findViewById(R.id.instructionslayout);
    switch(setv.getId())
    {
        case R.id.changecolorbtn:
            int id = SelectedRadioButton();
            switch(id)
            {
                case 1:toost("Original");break;
                case 2:toost("Grey");instructionlayout.setBackgroundResource(R.color.grey);break;//problem is here
                case 3:toost("Red");break;
                case 4:toost("Orange");break;
                case 5:toost("Yellow");break;
                case 6:toost("Green");break;
                case 7:toost("Aqua");break;
                case 8:toost("Marine");break;
                case 9:toost("Purple");break;
                case 10:toost("Silver");break;
                default:toost("Error! - Select An Option!");
            }
            break;
    }
}
public int SelectedRadioButton()
{
    RadioButton original = (RadioButton)findViewById(R.id.origcolor);
    RadioButton grey = (RadioButton)findViewById(R.id.grey);
    RadioButton red = (RadioButton)findViewById(R.id.red);
    RadioButton orange = (RadioButton)findViewById(R.id.orange);
    RadioButton yellow = (RadioButton)findViewById(R.id.yellow);
    RadioButton green = (RadioButton)findViewById(R.id.green);
    RadioButton aqua = (RadioButton)findViewById(R.id.aqua);
    RadioButton marine = (RadioButton)findViewById(R.id.marine);
    RadioButton purple = (RadioButton)findViewById(R.id.purple);
    RadioButton silver = (RadioButton)findViewById(R.id.silver);
    int starter = 1;
    switch(starter)//take advantage of switch falling without break statement
    {
        case 1:if(original.isChecked()){return 1;}
        case 2:if(grey.isChecked()){return 2;}
        case 3:if(red.isChecked()){return 3;}
        case 4:if(orange.isChecked()){return 4;}
        case 5:if(yellow.isChecked()){return 5;}
        case 6:if(green.isChecked()){return 6;}
        case 7:if(aqua.isChecked()){return 7;}
        case 8:if(marine.isChecked()){return 8;}
        case 9:if(purple.isChecked()){return 9;}
        case 10:if(silver.isChecked()){return 10;}
    }
    return -1;//if nothing is checked
}

colors.xml:

<resources>
<color name="original">#25383C</color>
<color name="grey">#484849</color>
<color name="red">#881A27</color>
<color name="orange">#ffa500</color>
<color name="yellow">#CDE707</color>
<color name="green">#00ff00</color>
<color name="aqua">#00FFCC</color>
<color name="marine">#0C0C84</color>
<color name="purple">#630A86</color>
<color name="silver">#c0c0c0</color>
</resources>

日志貓:

10-22 10:32:49.834  17834-17834/dhruvyadav.msplit E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: dhruvyadav.msplit, PID: 17834
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3983)
        at android.view.View.performClick(View.java:4654)
        at android.view.View$PerformClick.run(View.java:19438)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5602)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.view.View$1.onClick(View.java:3978)

我努力了:

 View instructionlayout = (View)findViewById(R.id.instructionslayout);
 instructionlayout.setBackgroundColor(R.color.grey);

如何在.setBackground代碼之間放置ui線程?

您使用setBackgroundColor

yourView.setBackgroundColor(R.color.your_color);

用這個:

instructionlayout.setBackgroundColor(Color.GRAY);

代替:

instructionlayout.setBackgroundResource(R.color.grey);

嘗試這個

int color = getResources().getColor(R.color.original);
instructionlayout.setBackgroundColor(color);

要么

instructionlayout.setBackgroundResource(R.color.original);

請檢查RelativeLayout的ID是否與您引用的ID相同。

R.id.instructionslayout可以是R.id.instructionlayout例如對象名稱。

請使用以下代碼。.它對我有用..我希望它將對您有幫助...

用這個

指令layout.setBackgroundColor(getResources()。getColor(R.color.white));

代替

說明layout.setBackgroundResource(R.color.grey);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM