2016-05-13 61 views
0

因此,当我尝试设置自定义适配器时,我经常会收到空指针。我无法弄清楚什么是错的。得到一个错误,说这主要是代码,所以这个文本很容易填满空间。ListAdapter setAdapter返回null

代码在那里我设置适配器:

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:

 
FATAL EXCEPTION: main 

Process: com.example.j.airportmeet, PID: 2
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) 

<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的堆栈跟踪

回答

1

问题就在这里:

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);