空指针异常请在Android

问题描述:

XML代码SetBackgroundColour方法:空指针异常请在Android

<RelativeLayout 
    android:layout_width="193dp" 
    android:layout_height="100dp" 
    android:background="@color/RED" 
    android:layout_alignParentBottom="true" 
    android:id="@+id/butt1"></RelativeLayout> 
<RelativeLayout 
    android:layout_width="193dp" 
    android:layout_height="100dp" 
    android:background="@color/YELLOW" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:id="@+id/butt2"></RelativeLayout> 
<RelativeLayout 
    android:layout_width="193dp" 
    android:layout_height="100dp" 
    android:background="@color/BLUE" 
    android:layout_above="@+id/butt2" 
    android:layout_alignParentRight="true" 
    android:id="@+id/butt3"></RelativeLayout> 
<RelativeLayout 
    android:layout_width="193dp" 
    android:layout_height="100dp" 
    android:background="@color/GREEN" 
    android:layout_above="@+id/butt1" 
    android:id="@+id/butt4"></RelativeLayout> 

Java代码:

butt4 = (RelativeLayout)findViewById(R.id.butt4); 
    butt3 = (RelativeLayout)findViewById(R.id.butt3); 
    butt1 = (RelativeLayout)findViewById(R.id.butt1); 
    butt2 = (RelativeLayout)findViewById(R.id.butt2); 
    butt1.setBackgroundColor(Color.RED); 
    butt2.setBackgroundColor(Color.BLUE); 
    butt3.setBackgroundColor(Color.GREEN); 
    butt4.setBackgroundColor(Color.YELLOW); 

日志:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 
'void android.view.View.setBackgroundColor(int)' on a null object reference 
                     at com.example.chirag.red.Hard.change(Hard.java:73) 
                     at com.example.chirag.red.Hard.onCreate(Hard.java:53) 
                     at android.app.Activity.performCreate(Activity.java:6672) 
                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140) 
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)  
                     at android.app.ActivityThread.-wrap12(ActivityThread.java)  
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)  
                     at android.os.Handler.dispatchMessage(Handler.java:102)  
                     at android.os.Looper.loop(Looper.java:154)  
                     at android.app.ActivityThread.main(ActivityThread.java:6123)  
                     at java.lang.reflect.Method.invoke(Native Method)  
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)  
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 

的应用程序崩溃,每到这个活动启动。该错误总是在行“butt1.setBackgroundColor()”,它给了我一个空指针异常。我给布局一个ID,引用它,并且还传递了一种颜色作为方法中的参数。我真的不知道我哪里出了问题。请帮忙!

这是做正确的方式:

butt1.setBackgroundColor(getResources().getColor(R.color.red)); 

你必须定义颜色在你colors.xml

<color name="red">#ff0000</color> 
+0

显示完全相同的错误! – Chirag

+0

你的xml还有什么?是父母包围的相对布局吗? – DroiDev

+0

还有别的东西你没有向我们展示。我运行了一个测试应用程序,我给你的代码工作。所以更多的是它比你告诉我们的更多。 – DroiDev

你有一个空......然后可能会有一些原因

findViewById(xxxx)返回null。

一个 - 检查是否是correcto您:

android:id="@+id/xxx"> 

两个 - Java代码对应的XML?可以作出不同的参考..

把完整的代码。

+0

我的不好。我已将布局设置为另一个类似的布局。谢谢你的帮助! – Chirag

+0

我知道只要选择正确的答案来帮助别人。 – josedlujan