2016-05-13 11 views
0

So bekomme ich immer einen Nullpointer, wenn ich versuche, meinen benutzerdefinierten Adapter zu setzen. Ich kann nicht herausfinden, was falsch ist. Einen Fehler zu bekommen, der das sagt, ist meistens Code, also ist dieser Text einfach, Raum zu füllen.ListAdapter setAdapter gibt null zurück

-Code, wo ich die Einstellung der Adapter:

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

individuelle Adapter:

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> 

stacktrace von logcat:

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

Antwort

1

Die Frage ist:

ListView listView = (ListView) getView().findViewById(R.id.dialogListView); 

Ich weiß nicht genau, was getView() ist, aber ich weiß, dass Ihre Listview nicht da ist. Sie haben hier eine Ansicht

View dialogLayout = inflater.inflate(R.layout.dialoglist, null); 

erstellt und sollten ListView in dieser Ansicht suchen.

ListView listView = (ListView) dialogLayout.findViewById(R.id.dialogListView);