0

I have an adapter called PaymentMethodAdapter. I am trying to access it's parent view element, which is already there in xml.

ViewGroup parentView;
LinearLayout listView;

public View getView(int position, View convertView, ViewGroup parent) {
    parentView = parent;
    listView = (LinearLayout) parentView.findViewById(R.id.ben_pay_methods);
}

I am getting nullpointer exception error:

TRACE

01-20 13:22:47.075    6569-6569/com.example.android.mtesapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.mtesapp, PID: 6569
java.lang.NullPointerException
        at com.example.android.mtesapp.PaymentMethodAdapter.getView(PaymentMethodAdapter.java:65)
        at com.example.android.mtesapp.EditBeneficiaryActivity.show_receiving_methods(EditBeneficiaryActivity.java:315)
        at com.example.android.mtesapp.EditBeneficiaryActivity$AsyncTaskGetBenPaymentMethods.onPostExecute(EditBeneficiaryActivity.java:299)
        at com.example.android.mtesapp.EditBeneficiaryActivity$AsyncTaskGetBenPaymentMethods.onPostExecute(EditBeneficiaryActivity.java:283)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5872)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
        at dalvik.system.NativeStart.main(Native Method)

How can I check the elements in the parent view exist or not?

EDIT

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="50dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            style="@style/panelLayout"
            android:background="@color/white"
            android:id="@+id/panel1"
            >

            <RelativeLayout
                style="@style/panelFrame"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageButton
                    android:layout_height="120dp"
                    android:layout_width="120dp"
                    android:id="@+id/ben_pic"
                    android:src="@drawable/bee"
                    android:background="@color/white"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="John Doe"
                    android:id="@+id/ben_first_name"
                    android:layout_alignTop="@+id/ben_pic"
                    android:layout_toRightOf="@+id/ben_pic"
                    android:layout_toEndOf="@+id/ben_pic"
                    android:layout_marginLeft="30dp"
                    android:layout_marginStart="30dp"
                    android:hint="First Name"

                    android:background="@drawable/blue_edit_text_holo_light"/>

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="John Doe"
                    android:id="@+id/ben_last_name"
                   android:layout_below="@+id/ben_first_name"
                    android:layout_toRightOf="@+id/ben_pic"
                    android:layout_toEndOf="@+id/ben_pic"
                    android:layout_marginLeft="30dp"
                    android:layout_marginStart="30dp"
                    android:hint="Last Name"
                    android:background="@drawable/blue_edit_text_holo_light"
                   />
               <!-- <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="someone@something.com"
                    android:id="@+id/ben_email"
                    android:hint="@string/email"
                    android:layout_below="@+id/ben_last_name"
                    android:layout_toRightOf="@+id/ben_pic"
                    android:layout_toEndOf="@+id/ben_pic"
                    android:layout_marginLeft="30dp"
                    android:layout_marginStart="30dp"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:background="@drawable/blue_edit_text_holo_light"/>-->
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="647-777-0000"
                    android:hint="@string/phone"
                    android:id="@+id/ben_phone1"
                    android:layout_below="@+id/ben_last_name"
                    android:layout_toRightOf="@+id/ben_pic"
                    android:layout_toEndOf="@+id/ben_pic"
                    android:layout_marginLeft="30dp"
                    android:layout_marginStart="30dp"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:background="@drawable/blue_edit_text_holo_light"/>

                <Button
                    style="@style/BtnDanger"
                    android:layout_width="wrap_content"
                    android:text="Delete"
                    android:id="@+id/delete_ben_btn"
                    android:layout_below="@+id/ben_phone1"
                    android:layout_toRightOf="@+id/ben_pic"
                    android:layout_toEndOf="@+id/ben_pic"
                    android:layout_height="40dp"
                    android:layout_marginLeft="30dp"
                    android:layout_marginStart="30dp"
                    android:textAppearance="?android:attr/textAppearanceSmall"
/>
            </RelativeLayout>


        </LinearLayout>



        <LinearLayout
            style="@style/panelLayout"
            android:id="@+id/panel2">
            <TextView
                style="@style/title_inside"
                android:text="@string/address" />


                <EditText
                    style="@style/Base.Widget.AppCompat.EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/ben_address1"
                    android:text="123 Something Ave"
                    android:hint="@string/address"
                    android:background="@drawable/blue_edit_text_holo_light">

                </EditText>

                <!--<TextView
                    style="@style/LabelEdit"
                    android:text="@string/address" />-->



                <EditText
                    style="@style/Widget.AppCompat.EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/ben_address2"
                    android:text=""
                    android:hint="@string/address2"
                    android:background="@drawable/blue_edit_text_holo_light">


                </EditText>

                <EditText
                    style="@style/Widget.AppCompat.EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/ben_city"
                    android:text="Toronto"
                    android:hint="@string/city"
                    android:background="@drawable/blue_edit_text_holo_light">

                </EditText>

                <EditText
                    style="@style/Widget.AppCompat.EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/ben_province"
                    android:text="Ontario"
                    android:hint="@string/province"
                    android:background="@drawable/blue_edit_text_holo_light">

                </EditText>

                <TextView
                    style="@style/Widget.AppCompat.EditText"
                    android:id="@+id/ben_country"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/country"
                    android:background="@drawable/blue_edit_text_holo_light"/>


        </LinearLayout>

        <LinearLayout
            style="@style/panelLayout"
            android:id="@+id/panel3">
            <TextView
                style="@style/title_inside"
                android:text="@string/receive_method" />
            <LinearLayout
                android:orientation="vertical"
                android:id="@+id/ben_pay_methods"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/layout1"
                >
                </LinearLayout>

            <Button
                style="@style/BtnInfo"
                android:id="@+id/btn_add_new_method"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="add_new_method"
                android:text="@string/add_new_method"/>



        </LinearLayout>
    </LinearLayout>
</ScrollView>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="4"
    android:layout_alignParentBottom="true">
    <Button
        style="@style/BtnPrimary"
        android:id="@+id/btn_edit_beneficiary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/save"/>
</LinearLayout>

<ProgressBar
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>
RNK
  • 5,582
  • 11
  • 65
  • 133
  • can you provide the hole XML file which contains the ParentView – Fouad Wahabi Jan 20 '15 at 18:38
  • what makes you think that `parentView` is not null ? (hint: according to the stacktrace, it is.) – njzk2 Jan 20 '15 at 19:29
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Jan 20 '15 at 19:29
  • @RonakPatel I don't see any thing in your XML file which works with adapters , are you working with `AdapterView` , because the ParentView is setted once you invoked the `setAdapter` on an `AdapterView` – Fouad Wahabi Jan 20 '15 at 20:24

2 Answers2

0

use convertView for inflating any view from item view of list

public View getView(int position, View convertView, ViewGroup parent) {

convertView = getActivity().getLayoutInflater().inflate(R.layout.your_xml, null);
        listView = (LinearLayout) convertView.findViewById(R.id.ben_pay_methods);
}
Kavis
  • 98
  • 4
  • the xml you have pasted above is your list item, right ? – Kavis Jan 20 '15 at 19:00
  • What do you mean by list item? – RNK Jan 20 '15 at 19:12
  • public View getView(int position, View convertView, ViewGroup parent) this method belong to adapter. So in adapter we always inflate our list view item and set their value according to it. – Kavis Jan 20 '15 at 19:13
  • It says that I can not call `getActivity()` here.. can not resolve method – RNK Jan 20 '15 at 19:42
0

As @Fouad Wahabi has already suggested in the comment, I found your layout XML is invalid.

There is no View with id of layout1. And also, though the LinearLayout is not a child View of RelativeLayout, it has an android:layout_below attibute. This can cause the error.

Try to remove the attribute like this:

<LinearLayout
    android:orientation="vertical"
    android:id="@+id/ben_pay_methods"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</LinearLayout>
hata
  • 11,633
  • 6
  • 46
  • 69