像这样的愚蠢错误让我发疯,只想为每个视图绘制两种颜色的布局的背景,但我得到了一个 nullPointerException。
XML 文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/background_areas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
....
JAVA文件:
for (int o = 0; o < areas.size(); o++) {
LayoutInflater li_area= (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View vArea = li_area.inflate(R.layout.areas_detalles_cpa, null);
RelativeLayout fondo_area = (RelativeLayout)findViewById(R.id.background_areas);
if(o%2==0){
空指针异常fondo_area.setBackgroundColor(Color.rgb(104, 2, 64));
}else{
fondo_area.setBackgroundColor(Color.rgb(197, 5, 120));
}
......
日志猫:
01-31 14:11:47.956: W/System.err(12950): java.lang.NullPointerException
01-31 14:11:47.964: W/System.err(12950): at com.hiberus.campus.cpa.views.AreasEstudiosCPA.cargarListaAreas(AreasEstudiosCPA.java:125)
01-31 14:11:47.964: W/System.err(12950): at com.hiberus.campus.cpa.views.AreasEstudiosCPA$task.onPostExecute(AreasEstudiosCPA.java:200)
01-31 14:11:47.964: W/System.err(12950): at com.hiberus.campus.cpa.views.AreasEstudiosCPA$task.onPostExecute(AreasEstudiosCPA.java:1)
01-31 14:11:47.964: W/System.err(12950): at android.os.AsyncTask.finish(AsyncTask.java:602)
01-31 14:11:47.964: W/System.err(12950): at android.os.AsyncTask.access$600(AsyncTask.java:156)
01-31 14:11:47.964: W/System.err(12950): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
01-31 14:11:47.964: W/System.err(12950): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 14:11:47.964: W/System.err(12950): at android.os.Looper.loop(Looper.java:137)
01-31 14:11:47.964: W/System.err(12950): at android.app.ActivityThread.main(ActivityThread.java:4514)
01-31 14:11:47.964: W/System.err(12950): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 14:11:47.964: W/System.err(12950): at java.lang.reflect.Method.invoke(Method.java:511)
01-31 14:11:47.964: W/System.err(12950): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-31 14:11:47.964: W/System.err(12950): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-31 14:11:47.964: W/System.err(12950): at dalvik.system.NativeStart.main(Native Method)
我是否检查了 id、xml 文件的名称并且一切似乎都很好,这正在发生吗?