2016-06-01 9 views
-1

Ich brauche Hilfe Ich habe versucht, einen Weg zu finden, wie benutzerdefinierte Listenansicht implementieren und ich erhalte eine Fehlermeldung: -Fehler bei Implementierung eigene Listenansicht mit BaseAdapter

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.edis.edis.HospitalAdaptor.getView(HospitalAdaptor.java:62)

Hier finden Sie das Objekt, das Ich habe erstellt und es heißt Data.java.

public class Data { 

private double lat, lon; 
public String name; 
String groupA, groupB, groupAB,groupO; 
public int url_image; 


public Data(double lat, double lon, int url_image, String name, String groupA, String groupB, String groupAB, String groupO) 
{ 
    this.lat =lat; 
    this.lon = lon; 
    this.url_image =url_image; 
    this.name = name; 
    this.groupA =groupA; 
    this.groupB =groupB; 
    this.groupAB = groupAB; 
    this.groupO = groupO; 
} 
} 

Dann habe ich einen Adapter erstellt, wo ich einen Fehler (Null-Objekt) erhalten auf der Linie, wo chakula.setText((hospital.name)) ist.

Folgendes ist der Adapter, den ich BaseAdaptor verwendet habe. HospitalsAdaptors.java

public class HospitalAdaptor extends BaseAdapter{ 
    Context context; 
    List<Data> datas; 


    public HospitalAdaptor(Context context, List<Data> datas) { 

     this.context = context; 
     this.datas = datas; 

    } 


    @Override 
    public int getCount() { 
     return datas.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return datas.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     Data hospital = (Data)getItem(position); 

     if(convertView == null){ 

      convertView = LayoutInflater.from(context).inflate(R.layout.fragment_hospitals,parent,false); 
      //convertView = LayoutInflater.inflate(R.layout.fragment_hospitals,parent,false); 
     } 

     TextView chakula = (TextView)convertView.findViewById(R.id.hospitals_name); 
     ImageView pilau = (ImageView)convertView.findViewById(R.id.hospitals_image); 

     chakula.setText(hospital.name); 

     Glide 
       .with(context) 
       .load(hospital.url_image) 
       .centerCrop() 
       .crossFade() 
       .into(pilau); 

     return convertView; 
    } 
} 
+0

Warum verwenden Sie nicht 'ArrayAdapter'? – pskink

+0

Hast du keine Getter und Setter? –

+0

Bitte posten Sie die Layout-Datei auch –

Antwort

0

diese Methode ersetzen und Getter und Setter in Ihrer Datenklasse erzeugen.

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    Data hospital = (Data)datas.get(position); 

    if(convertView == null){ 

     convertView = LayoutInflater.from(context).inflate(R.layout.fragment_hospitals,parent,false); 
     //convertView = LayoutInflater.inflate(R.layout.fragment_hospitals,parent,false); 
    } 

    TextView chakula = (TextView)convertView.findViewById(R.id.hospitals_name); 
    ImageView pilau = (ImageView)convertView.findViewById(R.id.hospitals_image); 

    chakula.setText(hospital.getName()); 

    Glide 
      .with(context) 
      .load(hospital.url_image) 
      .centerCrop() 
      .crossFade() 
      .into(pilau); 

    return convertView; 
} 
+0

können Sie bitte helfen – user3337660

+0

@ user3337660 ya – anonymous

+0

generieren Setter und Getter-Methode in Ihrer Data-Klasse zuerst – anonymous

0
you have just initialize variable not set value 

it should be like this 

     public Data(double lat, double lon, int url_image, String name, String groupA, String groupB, String groupAB, String groupO) 
    { 
     this.lat =lat; 
     this.lon = lon; 
     this.url_image =url_image; 
     this.name = name; 
     this.groupA =groupA; 
     this.groupB =groupB; 
     this.groupAB = groupAB; 
     this.groupO = groupO; 

setname(this.name) // same apply to all other 
    } 
+0

ich nehme an das ist, wie es funktionieren sollte können Sie helfen ?? – user3337660

0

wie dieses Versuchen

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    if(convertView == null){ 

     convertView = LayoutInflater.from(context).inflate(R.layout.fragment_hospitals,parent,false); 
     //convertView = LayoutInflater.inflate(R.layout.fragment_hospitals,parent,false); 
    } 
    Data hospital = datas.get(position); 

    TextView chakula = (TextView)convertView.findViewById(R.id.hospitals_name); 
    ImageView pilau = (ImageView)convertView.findViewById(R.id.hospitals_image); 

    chakula.setText(hospital.name); 

    Glide.with(context) 
      .load(hospital.url_image) 
      .centerCrop() 
      .crossFade() 
      .into(pilau); 

    return convertView; 
} 
+0

Es funktioniert nicht .. – user3337660

Verwandte Themen