2016-05-01 16 views
1

Ich versuche, ein Bild und einen Text für jedes Listenansichtselement anzuzeigen. Ich habe ein paar Tutorials gelesen, konnte es aber immer noch nicht.Versuch zum Aufrufen der virtuellen Methode 'void ListView.setAdapter (ListAdapter)' für eine Nullobjekt-Referenz

Ich erhalte diese Störung, wenn ich versuche, TagsActivity zu öffnen:

Versuch virtuelle Methode 'Leere android.widget.ListView.setAdapter (android.widget.ListAdapter)' auf einem null aufzurufen Objektreferenz

In dieser Zeile:

this.tagList.setAdapter(adapter); 

TagsAct ivity.java

public class TagsActivity extends AppCompatActivity { 
public ListView tagList; 
final List<Tag> tags = new ArrayList<>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.activity_main); 

    tags.add(new Tag("Black & White")); 
    tags.add(new Tag("Geometric")); 

    this.tagList = (ListView) this.findViewById(R.id.list_tags); 
    LazyAdapter adapter = new LazyAdapter(this, this.tags); 
    this.tagList.setAdapter(adapter); 
} 
} 

Tag.java

public class Tag { 
private String title; 

public Tag(String title) { 
    super(); 
    this.title = title; 
} 

public String getTitle() { 
    return this.title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 
} 

JazyAdapter.java

public class LazyAdapter extends BaseAdapter { 
private LayoutInflater inflater; 
private List<Tag> tags; 

public LazyAdapter(Activity activity, List<Tag> tags) { 
    this.inflater = (LayoutInflater) activity.getSystemService(
      Context.LAYOUT_INFLATER_SERVICE); 
    this.tags = tags; 
} 

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

@Override 
public Tag getItem(int position) { 
    return this.tags.get(position); 
} 

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

@Override 
public View getView (int position, View convertView, ViewGroup parent) { 
    View row; 
    row = this.inflater.inflate(R.layout.list_item, null); 

    TextView textView = (TextView) row.findViewById(R.id.title); 
    ImageView imageView = (ImageView) row.findViewById(R.id.icon); 

    Tag tag = this.tags.get(position); 

    textView.setText(tag.getTitle()); 
    imageView.setImageResource(R.drawable.icon); 

    return row; 
} 
} 

list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:padding="15dp" 
android:background="@drawable/border_bottom"> 

<LinearLayout 
    android:id="@+id/thumb" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="2dp" 
    android:layout_marginRight="5dp" 
    android:layout_alignParentLeft="true"> 
    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="35dp" 
     android:layout_height="35dp" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="5dp" 
     android:src="@drawable/icon" /> 
</LinearLayout> 

<TextView 
    android:id="@+id/title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/thumb" 
    android:layout_toRightOf="@+id/thumb" 
    android:layout_toEndOf="@+id/thumb" 
    android:layout_centerVertical="true" 
    android:textStyle="bold" 
    android:textSize="24sp" 
    android:text="Black &amp; White" 
    android:paddingTop="5dp"/> 

<ImageView 
    android:layout_width="20dp" 
    android:layout_height="20dp" 
    android:layout_margin="15dp" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:src="@drawable/arrow_right"/> 
</RelativeLayout> 

activity_tags.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="{relativePackage}.${activityClass}" > 

<ListView 
    android:id="@+id/list_tags" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
</RelativeLayout> 

Wie kann ich dieses Problem lösen?

Antwort

0

Sie bekommen diesen Fehler, da der "View Zeile" null ist. ersetzen, die nur in Ihrem LazyLoader.java

View row; 
row = this.inflater.inflate(R.layout.list_item, null); 

mit:

View row = convertView; 
if (row == null) { 
    row = this.inflater.inflate(R.layout.list_item, null); 
} 
+1

Anstatt nur eine Klasse zu veröffentlichen, möchten Sie vielleicht erklären, warum dies das Problem lösen würde, wie in der Frage gefragt. So kann das Plakat daraus lernen, ebenso wie zukünftige Benutzer. –

+0

Weil mein Englisch ist sehr schlecht = ( –

0

this.setContentView(R.layout.activity_main); sollte this.setContentView(R.layout.activity_tags);

+0

Danke, ich denke, das war ein Fehler, aber ich habe immer gleiche Fehler. :( –

Verwandte Themen