-1

Ich versuche, ein Dialog-Fragment mit einer Liste aller Kontakte dort zu zeigen.Layout inflater auf ein benutzerdefiniertes Dialog-Fragment gibt eine Null-Zeiger-Ausnahme zurück

ich diesen Adapter gemacht haben:

package fr.nf.smsplus.adapter; 

import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

import java.util.List; 

import fr.nf.smsplus.object.Contact; 
import fr.nf.smsplus.R; 
import fr.nf.smsplus.contactsDialogFragment; 

/** 
* Created by Nicolas on 22/05/2016. 
*/ 
public class contactListAdapter extends BaseAdapter { 

    private List<Contact> liste; 
    private static LayoutInflater inflater = null; 

    public contactListAdapter(contactsDialogFragment contactsDialogFragment, List<Contact> liste) { 
     this.liste = liste; 
     //inflater = (LayoutInflater) contactsDialogFragment.getSystemService(contactsDialogFragment.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return liste.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return liste.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     Contact contact = liste.get(position); 
     View vi = convertView; 
     if (vi == null) 
      vi = inflater.inflate(R.layout.item_contact, null); 
     TextView text = (TextView) vi.findViewById(R.id.contact_name); 
     text.setText(contact.getDisplayName()); 
     return vi; 
    } 
} 

ich diese dialogfragment gemacht habe:

package fr.nf.smsplus; 

import android.app.AlertDialog; 
import android.app.Dialog; 
import android.app.DialogFragment; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.AbsListView; 

import java.util.List; 

import fr.nf.smsplus.adapter.contactListAdapter; 
import fr.nf.smsplus.object.Contact; 

/** 
* Created by Nicolas on 22/05/2016. 
*/ 
public class contactsDialogFragment extends DialogFragment{ 

    List<Contact> liste; 
    AbsListView contactList; 

    public static contactsDialogFragment newInstance(List<Contact> liste){ 
     contactsDialogFragment retour = new contactsDialogFragment(); 
     retour.liste = liste; 
     return retour; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the Builder class for convenient dialog construction 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

     LayoutInflater inflater = getActivity().getLayoutInflater(); 
     View contactListView = inflater.inflate(R.layout.dialog_contacts, null); 
     contactList = (AbsListView) contactListView.findViewById(R.id.listContacts); 
     contactList.setAdapter(new contactListAdapter(this, liste)); 

     builder.setView(contactListView) 
       .setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // User cancelled the dialog 
        } 
       }) 
       .setTitle(R.string.cdf_title);; 



     // Create the AlertDialog object and return it 
     return builder.create(); 
    } 
} 

ich den Dialog wie folgt aufgerufen:

fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       contactList = new ArrayList<Contact>(); 
       listContacts(); 

       contactsDialogFragment pickContact = contactsDialogFragment.newInstance(contactList); 
       pickContact.show(getFragmentManager(), "contacts"); 
      } 
     }); 

Aber ich werde immer habe diesen Fehler:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup)' on a null object reference at fr.nf.smsplus.adapter.contactListAdapter.getView(contactListAdapter.java:52) 

Wie kann ich dieses Problem beheben? Gibt es ein Problem der Ausführungszyklus (versucht, ein inexisting Element aufzublasen)

EDIT

Der Versuch, Malik Weg, ich habe meine Adapter modifiziert, um den Kontext zu schreiben, und ich habe sameerror:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference at fr.nf.smsplus.adapter.contactListAdapter.getView(contactListAdapter.java:55) 

Ich versuche es auch: contactList.setAdapter(new contactListAdapter(getActivity(), liste)); Und es wird nicht funktionieren! Ändere einfach auch meinen Adapterkonstruktor zu public contactListAdapter(Context context, List<Contact> liste)

+1

entfernen diese und schreiben getActivity() in contactList.setAdapter (neu contactListAdapter (diese, liste)) declration wie diese contactList.setAdapter (neu contactListAdapter (getActivity(), liste)); –

Antwort

0

Versuchen Sie, die vi Ansicht von getView Ihres Adapters wie folgt aufzublasen.

if(vi==null){ 
    inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    vi=inflater.inflate(R.layout.item_contact,parent,false); 
} 
+0

Möchten Sie versuchen, aber was ist "c"? –

+0

Entschuldigung. Nur kopiert von und altes Stück Code. Es ist ein Kontext. –

+0

c ist wahrscheinlich ein Kontext, also füge ich es in die private Deklaration, xill try –