因此,当我尝试设置自定义适配器时,我经常会得到一个nullpointer。我无法弄清楚什么是错的。得到一个错误说这主要是代码所以这个文本很容易填补空间。
我设置适配器的代码:
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;
}
}
dialoglist.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的stacktrace:
FATAL EXCEPTION: main Process: com.example.j.airportmeet, PID: 20123 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.example.j.airportmeet.flightFragment$11.onClick(flightFragment.java:297) at android.view.View.performClick(View.java:5201) at android.view.View$PerformClick.run(View.java:21163) at android.os.Handler.handleCallback(Handler.java:746) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
答案 0 :(得分:2)
问题在于:
ListView listView = (ListView) getView().findViewById(R.id.dialogListView);
我不确切知道getView()
到底是什么,但我知道你的ListView并不存在。您已在此处创建了视图
View dialogLayout = inflater.inflate(R.layout.dialoglist, null);
并应在此视图中搜索ListView。
ListView listView = (ListView) dialogLayout.findViewById(R.id.dialogListView);