我有一个名为PaymentMethodAdapter
的适配器。我正在尝试访问它的父视图元素,它已经存在于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);
}
我收到nullpointer exception
错误:
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)
如何检查父视图中的元素是否存在?
修改
<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>
答案 0 :(得分:0)
使用convertView从列表的项目视图中扩充任何视图
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);
}
答案 1 :(得分:0)
正如@Fouad Wahabi已经在评论中建议的那样,我发现你的布局XML无效。
没有ID为 layout1 的视图。而且,虽然LinearLayout不是RelativeLayout的子视图,但它有一个 android:layout_below attibute。这可能会导致错误。
尝试删除这样的属性:
<LinearLayout
android:orientation="vertical"
android:id="@+id/ben_pay_methods"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>