【发布时间】:2016-09-10 05:40:10
【问题描述】:
所以当我尝试设置我的自定义适配器时,我不断地得到一个空指针。我不知道出了什么问题。收到一个错误,说这主要是代码,所以这个文本很容易填满空间。
我设置适配器的代码:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select a match");
//insert array to constructor
LayoutInflater inflater = getLayoutInflater(savedInstanceState);
View dialogLayout = inflater.inflate(R.layout.dialoglist, null);
ListAdapter testAdapter = new matchAdapter(getActivity().getApplicationContext(), userInfos);
ListView listView = (ListView) getView().findViewById(R.id.dialogListView);
listView.setAdapter(testAdapter);
builder.setView(dialogLayout);
AlertDialog alert = builder.create();
alert.show();
自定义适配器:
class matchAdapter extends ArrayAdapter<userInfo> {
public matchAdapter(Context context, ArrayList<userInfo> test) {
super(context, R.layout.match_result, test);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customRow = inflater.inflate(R.layout.match_result, parent, false);
userInfo singleItemTest = getItem(position);
/*
TODO: get references to layout elements
*/
TextView username = (TextView) customRow.findViewById(R.id.matchUsername);
TextView sex = (TextView) customRow.findViewById(R.id.matchSex);
TextView age = (TextView) customRow.findViewById(R.id.matchAge);
Button sendRequest = (Button) customRow.findViewById(R.id.matchSendRequest);
username.setText(singleItemTest.getUserName());
return customRow;
}
}
对话列表.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialogListView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
来自 logcat 的堆栈跟踪:
致命例外:主要 进程:com.example.j.airportmeet,PID:20123 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.ListView.setAdapter(android.widget.ListAdapter)” 在 com.example.j.airportmeet.flightFragment$11.onClick(flightFragment.java:297) 在 android.view.View.performClick(View.java:5201) 在 android.view.View$PerformClick.run(View.java:21163) 在 android.os.Handler.handleCallback(Handler.java:746) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:148) 在 android.app.ActivityThread.main(ActivityThread.java:5443) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)【问题讨论】:
标签: android xml android-layout listview