简体   繁体   English

setBackgroundColor()似乎不起作用

[英]setBackgroundColor() doesn't seem to work

I have a RelativeLayout and a Button in my Activity I want to run a code when I click on the Button , so I wrote the following code: 我的Activity有一个RelativeLayout和一个Button ,当我单击Button ,我想运行一个代码,因此我编写了以下代码:

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:src="@drawable/flashlight"
    android:onClick="NightSwitch" />

and then I have the following in my Activity.java 然后我的Activity.java有以下内容

public void NightSwitch(View v){
    String One = "1"; //states day
    String Zero = "0"; //states night

    int color1 = getResources().getColor(R.color.darkblue);
    int color2 = getResources().getColor(R.color.white);

    RelativeLayout rele = (RelativeLayout) findViewById(R.id.RelativeLayout1);
    String state = getString(R.string.night_switch);

    if(state.equals(One)){
        rele.setBackgroundColor(color1);
    }

    if(state.equals(Zero)){
        rele.setBackgroundColor(color2);
    }
}

Everything is ok, but setBackgroundColor() does not work and causes a crash. 一切正常,但setBackgroundColor()无法正常工作并导致崩溃。 also, how can I debug program in a VB style? 另外,如何调试VB风格的程序? Is it even possible? 可能吗

Logcat : Logcat:

01-27 17:38:45.606: E/AndroidRuntime(21427): at android.view.View$1.onClick(View.java:3838) 01-27 17:38:45.606: E/AndroidRuntime(21427): at android.view.View.performClick(View.java:4475) 01-27 17:38:45.606: E/AndroidRuntime(21427): at android.view.View$PerformClick.run(View.java:18786) 01-27 17:38:45.606: E/AndroidRuntime(21427): at android.os.Handler.handleCallback(Handler.java:730) 01-27 17:38:45.606: E/AndroidRuntime(21427): at android.os.Handler.dispatchMessage(Handler.java:92) 01-27 17:38:45.606: E/AndroidRuntime(21427): at android.os.Looper.loop(Looper.java:137) 01-27 17:38:45.606: E/AndroidRuntime(21427): at android.app.ActivityThread.main(ActivityThread.java:5493) 01-27 17:38:45.606: E/AndroidRuntime(21427): at java.lang.reflect.Method.invokeNative(Native Method) 01-27 17:38:45.606: E/AndroidRuntime(21427): at java.lang.reflect.Method.invoke(Method.java:525) 01-27 17:38:45.606: E/AndroidRuntime(21427): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.jav 01-27 17:38:45.606:E / AndroidRuntime(21427):在android.view.View $ 1.onClick(View.java:3838)01-27 17:38:45.606:E / AndroidRuntime(21427):在Android .view.View.performClick(View.java:4475)01-27 17:38:45.606:E / AndroidRuntime(21427):在android.view.View $ PerformClick.run(View.java:18786)01-27 17 :38:45.606:E / AndroidRuntime(21427):在android.os.Handler.handleCallback(Handler.java:730)01-27 17:38:45.606:E / AndroidRuntime(21427):在android.os.Handler。 dispatchMessage(Handler.java:92)01-27 17:38:45.606:E / AndroidRuntime(21427):at android.os.Looper.loop(Looper.java:137)01-27 17:38:45.606:E / AndroidRuntime(21427):位于android.app.ActivityThread.main(ActivityThread.java:5493)01-27 17:38:45.606:E / AndroidRuntime(21427):位于java.lang.reflect.Method.invokeNative(本机方法) 01-27 17:38:45.606:E / AndroidRuntime(21427):at java.lang.reflect.Method.invoke(Method.java:525)01-27 17:38:45.606:E / AndroidRuntime(21427):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.jav a:1209) 01-27 17:38:45.606: E/AndroidRuntime(21427): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025) 01-27 17:38:45.606: E/AndroidRuntime(21427): at dalvik.system.NativeStart.main(Native Method) 01-27 17:38:45.606: E/AndroidRuntime(21427): Caused by: java.lang.reflect.InvocationTargetException 01-27 17:38:45.606: E/AndroidRuntime(21427): at java.lang.reflect.Method.invokeNative(Native Method) 01-27 17:38:45.606: E/AndroidRuntime(21427): at java.lang.reflect.Method.invoke(Method.java:525) 01-27 17:38:45.606: E/AndroidRuntime(21427): at android.view.View$1.onClick(View.java:3833) 01-27 17:38:45.606: E/AndroidRuntime(21427): ... 11 more 01-27 17:38:45.606: E/AndroidRuntime(21427): Caused by: java.lang.NullPointerException 01-27 17:38:45.606: E/AndroidRuntime(21427): at com.example.jonathanlseagull.NumOneActivity.NightSwitch(NumOneActivity.java:46) 01-27 17:38:45.606: E/AndroidRuntime(21427): ... 14 more a:1209)01-27 17:38:45.606:E / AndroidRuntime(21427):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)01-27 17:38:45.606:E / AndroidRuntime(21427):在dalvik.system.NativeStart.main(本机方法)01-27 17:38:45.606:E / AndroidRuntime(21427):由:java.lang.reflect.InvocationTargetException 01-27 17:38引起: 45.606:E / AndroidRuntime(21427):at java.lang.reflect.Method.invokeNative(本机方法)01-27 17:38:45.606:E / AndroidRuntime(21427):at java.lang.reflect.Method.invoke( Method.java:525)01-27 17:38:45.606:E / AndroidRuntime(21427):at android.view.View $ 1.onClick(View.java:3833)01-27 17:38:45.606:E / AndroidRuntime (21427):...还有11个01-27 17:38:45.606:E / AndroidRuntime(21427):原因:java.lang.NullPointerException 01-27 17:38:45.606:E / AndroidRuntime(21427):at com.example.jonathanlseagull.NumOneActivity.NightSwitch(NumOneActivity.java:46)01-27 17:38:45.606:E / AndroidRuntime(21427):...另外14个

onCreate(); onCreate();

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_num_one);
}

you should use 你应该使用

relativeLayout.setBackgroundResource(R.color.blue);

instead of 代替

relativeLayout.setBackgroundColor(color1);  // this method accepts Color not resource.

You must pass a Color ( http://developer.android.com/reference/android/graphics/Color.html ) attribute instead of the int code 您必须传递Color( http://developer.android.com/reference/android/graphics/Color.html )属性而不是int代码

if(state.equals(One))
{
    rele.setBackgroundColor(Color.parseColor("#4CB8FB"));
}
else
{
    rele.setBackgroundColor(Color.BLUE);
}

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

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