NullPointerException when draw background Relative

2019-07-30 14:10发布

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 FILE:

<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 FILE:

    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){

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)

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

2条回答
The star\"
2楼-- · 2019-07-30 15:04

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);
查看更多
女痞
3楼-- · 2019-07-30 15:05

Check that your areas_detalles_cpa layout includes fondo_area. Also, keep in mind that you can't find multiple views by the same ID in a loop the way you're doing.

Finding a view by ID only works if there's only one of those views with that ID, and you're doing it in a loop. You can do vArea.findViewById(R.id.background_areas) to get the specific view you need.

查看更多
登录 后发表回答