如何将我的方法链接到UI线程 - .setBackgroundColor();

时间:2015-10-22 17:36:51

标签: java android multithreading user-interface android-runonuithread

请每次按下changecolorbutton for grey,instructionlayout.setBackgroundColor();时,请帮我解释如何将方法setter()链接到UI线程。使我的应用程序崩溃。我所学到的是我需要将它链接到一个特殊的线程,以便它实际上对UI进行更改。这当前只是普通活动.class的一种方法,它扩展了ActionBarActivity。 我的代码:

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.setBackgroundColor(getResources().getColor(R.color.grey));break;
                    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>

我的相对布局ID:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="dhruvyadav.msplit.Instructions"
android:background="#25383C"
android:id="@+id/instructionslayout">
<。> .setBackgroungColor之后的日志cat错误(getResources()。getColor(R.color.grey));被激活:

10-22 23:04:15.304  17574-17574/dhruvyadav.msplit E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: dhruvyadav.msplit, PID: 17574
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)

我的类的开始,其中setter()方法位于:

public class Settings extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
}

1 个答案:

答案 0 :(得分:0)

请原谅我,但这段代码还不够。我认为你设置一个属性&#34; android:onClick&#34;在一定的观点。例如:

  

机器人:的onClick =&#34;的OnClick&#34;

public void onClick(View v) {

第一个字母中的错误。一个例外就像你一样。检查布局和处理函数的存在。