findViewById returns null when call this in a class that extend on RelativeLayout

709 Views Asked by At

I'm trying to get a layout with the findViewById method that I have in my activity_main from a class that I created. This class extends RelativeLayout and within the constructor I am trying to implement the onClick method for the user when they click on the RelativeLayout, this create another layout, so I need to get that layout to add to the view.

This is the code where I get the NPE:

public class PatientBoxLayout extends RelativeLayout {

/**
 * 
 */
private Patient patient;

/**
 * Constructor de la clase Patient Box
 * 
 * @param context
 * @param patient
 */

public static double coefi = 3.3;

public PatientBoxLayout(Context aContext, Patient aPatient) {
    super(aContext);

    setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            RelativeLayout horizontalLL = (RelativeLayout) findViewById(R.id.upp_layout);
            horizontalLL.addView(new PatientBoxLayout(v.getContext(),
                    ((PatientBoxLayout) v).getPatient()));
        }
    });

This is the XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:orientation="vertical" >

            <RelativeLayout
                android:id="@+id/upp_layout"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="258dp"
                    android:layout_marginTop="18dp"
                    android:text="Paciente" />

                <TextView
                    android:id="@+id/textViewNomPaciente"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/textView1"
                    android:layout_marginLeft="18dp"
                    android:layout_toRightOf="@+id/textView1"
                    android:text="TextView" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/textView1"
                    android:layout_alignRight="@+id/textView3"
                    android:layout_below="@+id/textView1"
                    android:layout_marginTop="15dp"
                    android:text="Edad" />

                <TextView
                    android:id="@+id/textViewEdadPaciente"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textView2"
                    android:layout_alignBottom="@+id/textView2"
                    android:layout_alignRight="@+id/textViewNomPaciente"
                    android:text="TextView" />

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/textView1"
                    android:layout_centerVertical="true"
                    android:text="Cama" />

                <TextView
                    android:id="@+id/textViewCamaPaciente"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textView3"
                    android:layout_alignBottom="@+id/textView3"
                    android:layout_alignLeft="@+id/textViewEdadPaciente"
                    android:text="TextView" />

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/textView3"
                    android:layout_below="@+id/textView3"
                    android:layout_marginTop="21dp"
                    android:text="Sexo" />

                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textView4"
                    android:layout_alignBottom="@+id/textView4"
                    android:layout_alignLeft="@+id/textViewCamaPaciente"
                    android:text="TextView" />

                <TextView
                    android:id="@+id/textView6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textViewNomPaciente"
                    android:layout_alignBottom="@+id/textViewNomPaciente"
                    android:layout_centerHorizontal="true"
                    android:text="NHC" />

                <TextView
                    android:id="@+id/textView7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textViewEdadPaciente"
                    android:layout_alignBottom="@+id/textViewEdadPaciente"
                    android:layout_alignLeft="@+id/textView6"
                    android:text="Episodio" />

                <TextView
                    android:id="@+id/textView8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textViewCamaPaciente"
                    android:layout_alignBottom="@+id/textViewCamaPaciente"
                    android:layout_alignLeft="@+id/textView7"
                    android:text="Diagnostico" />

                <TextView
                    android:id="@+id/textViewNHCPaciente"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textView6"
                    android:layout_alignBottom="@+id/textView6"
                    android:layout_marginLeft="33dp"
                    android:layout_toRightOf="@+id/textView8"
                    android:text="TextView" />

                <TextView
                    android:id="@+id/textViewEpisodioPaciente"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/textView7"
                    android:layout_alignLeft="@+id/textViewNHCPaciente"
                    android:text="TextView" />

                <TextView
                    android:id="@+id/textViewDiagnosticoPaciente"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textView8"
                    android:layout_alignBottom="@+id/textView8"
                    android:layout_alignLeft="@+id/textViewEpisodioPaciente"
                    android:text="TextView" />

                <TextView
                    android:id="@+id/textView9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textViewNHCPaciente"
                    android:layout_alignBottom="@+id/textViewNHCPaciente"
                    android:layout_marginLeft="79dp"
                    android:layout_toRightOf="@+id/textViewNHCPaciente"
                    android:text="FIngreso" />

                <TextView
                    android:id="@+id/textView10"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textViewEpisodioPaciente"
                    android:layout_alignBottom="@+id/textViewEpisodioPaciente"
                    android:layout_alignLeft="@+id/textView9"
                    android:text="Servicio" />

                <TextView
                    android:id="@+id/textView11"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/textViewDiagnosticoPaciente"
                    android:layout_alignLeft="@+id/textView10"
                    android:text="Alergias" />

                <TextView
                    android:id="@+id/textView12"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textView5"
                    android:layout_alignBottom="@+id/textView5"
                    android:layout_alignLeft="@+id/textView11"
                    android:text="Aislado" />

                <TextView
                    android:id="@+id/textViewFIngresoPaciente"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textView9"
                    android:layout_alignBottom="@+id/textView9"
                    android:layout_marginLeft="55dp"
                    android:layout_toRightOf="@+id/textView9"
                    android:text="TextView" />

                <TextView
                    android:id="@+id/textViewServicioPaciente"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textView10"
                    android:layout_alignBottom="@+id/textView10"
                    android:layout_alignLeft="@+id/textViewFIngresoPaciente"
                    android:text="TextView" />

                <TextView
                    android:id="@+id/textViewA"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textView11"
                    android:layout_alignBottom="@+id/textView11"
                    android:layout_alignLeft="@+id/textViewServicioPaciente"
                    android:text="TextView" />

                <TextView
                    android:id="@+id/textViewAisladoPaciente"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textView12"
                    android:layout_alignBottom="@+id/textView12"
                    android:layout_alignLeft="@+id/textViewA"
                    android:text="TextView" />
            </RelativeLayout>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"
            android:visibility="visible" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <ScrollView
                    android:id="@+id/scrollView1"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent" >

                    <LinearLayout
                        android:id="@+id/down_layout"
                        android:layout_width="match_parent"
                        android:layout_height="0dip"
                        android:orientation="horizontal" >
                    </LinearLayout>
                </ScrollView>
                <!-- Contenido principal -->

            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
    <!-- Contenido principal -->

    <ListView
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:background="#EEE"
        tools:listitem="@android:layout/simple_list_item_single_choice" >
    </ListView>
</android.support.v4.widget.DrawerLayout>

</RelativeLayout>

I hope you can help me identify the layout from the constructor of the class.

Thank you. Regards

1

There are 1 best solutions below

2
On BEST ANSWER

if you check on the docs: https://developer.android.com/reference/android/app/Activity.html and https://developer.android.com/reference/android/view/View.html both activity and view have a findViewById method.

So when you're calling findViewById(R.id.upp_layout); inside the PatientBoxLayout is trying to find that view only inside its own layout tree. And it can't find because it's not there. So it returns null and you crash with NullPointerException on the next line.

You can call getParent() to navigate up in the layout hierarchy and then call there the findViewById(R.id.upp_layout); or an even better solution is to not to put ClickListener inside a view or viewGroup. Those are not meant to handle clicks. You should handle the clicks on the Activity or Fragment.