绘制背景RelativeLayout时发生NullPointer

绘制背景RelativeLayout时发生NullPointer

本文介绍了绘制背景RelativeLayout时发生NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像这样的愚蠢错误使我发疯,只想为每个视图绘制两种颜色的布局背景,但是我得到了nullPointerException.

A silly mistake like this going me crazy, just want to paint the background of a layout of two colors for each view, but I get a 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){

NULLPOINTEREXCEPTION fondo_area.setBackgroundColor(Color.rgb(104, 2, 64));

NULLPOINTEREXCEPTIONfondo_area.setBackgroundColor(Color.rgb(104, 2, 64));

            }else{
                 fondo_area.setBackgroundColor(Color.rgb(197, 5, 120));
        }
......

Logcat:

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文件的名称以及一切似乎都很好,这是发生了吗?

Do I have checked the id, the names of the xml file and everything seems fine, that is happening?

推荐答案

我认为您缺少此部分.使用 vArea.findViewById()而不是仅使用 findViewById()

I think you are missing this part..Use vArea.findViewById() instead of only findViewById()

final View vArea = li_area.inflate(R.layout.areas_detalles_cpa, null);
        RelativeLayout fondo_area = (RelativeLayout)vArea.findViewById(R.id.background_areas);

这篇关于绘制背景RelativeLayout时发生NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 20:48