2016-05-01 81 views
1

我試圖顯示圖像和文本中的每個列表視圖項目。我讀了一些教程,但我仍然不能。嘗試在空對象引用上調用虛擬方法'void ListView.setAdapter(ListAdapter)'

試圖調用虛擬方法無效 android.widget.ListView.setAdapter(android.widget.ListAdapter)'上 空:

,當我嘗試打開TagsActivity我得到這個錯誤對象引用

在這一行:

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> 

我該如何解決這個問題?

回答

0

你得到,因爲「觀行」這個錯誤是零。只需更換您的LazyLoader.java

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

有了:

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

)而不是隻發佈一個班級,你可能想澄清爲什麼這樣可以解決問題中的問題,這樣海報可以從中學習,也可以從未來的用戶那裏學習。 –

+0

因爲我的英語非常糟糕=( –

0

this.setContentView(R.layout.activity_main);應該this.setContentView(R.layout.activity_tags);

+0

謝謝,我想這是一個錯誤,但我仍然得到同樣的錯誤。 :( –

相關問題