我的RelativeLayout
中有Button
和Activity
我想点击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" />
然后我的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);
}
}
setBackgroundColor()
不起作用并导致崩溃。另外,我如何以VB风格调试程序?它甚至可能吗?Logcat:
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):在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):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.java: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):引起: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):在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();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_num_one);
}
答案 0 :(得分:3)
你应该使用
relativeLayout.setBackgroundResource(R.color.blue);
而不是
relativeLayout.setBackgroundColor(color1); // this method accepts Color not resource.
答案 1 :(得分:0)
您必须传递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);
}