Java 尝试调用虚拟方法';void ListView.setAdapter(ListAdapter)和#x27;关于空对象引用
我试图在每个列表视图项中显示一个图像和文本。我读了一些教程,但还是看不懂 当我尝试打开TagsActivity时,出现以下错误: 尝试调用虚拟方法“void” android.widget.ListView.setAdapter(android.widget.ListAdapter)“”在 空对象引用 在这方面:Java 尝试调用虚拟方法';void ListView.setAdapter(ListAdapter)和#x27;关于空对象引用,java,android,Java,Android,我试图在每个列表视图项中显示一个图像和文本。我读了一些教程,但还是看不懂 当我尝试打开TagsActivity时,出现以下错误: 尝试调用虚拟方法“void” android.widget.ListView.setAdapter(android.widget.ListAdapter)“”在 空对象引用 在这方面: this.tagList.setAdapter(adapter); TagsActivity.java public class TagsActivity extends AppCo
this.tagList.setAdapter(adapter);
TagsActivity.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);
}
}
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;
}
}
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;
}
}
View row;
row = this.inflater.inflate(R.layout.list_item, null);
JazyAdapter.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);
}
}
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;
}
}
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;
}
}
View row;
row = this.inflater.inflate(R.layout.list_item, null);
公共类LazyAdapter扩展了BaseAdapter{
私人充气机;
私有列表标签;
公共LazyAdapter(活动、列表标记){
this.inflater=(LayoutInflater)activity.getSystemService(
上下文。布局(充气机和服务);
this.tags=标签;
}
@凌驾
public int getCount(){
返回此.tags.size();
}
@凌驾
公共标记getItem(int位置){
返回此.tags.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
查看行;
行=此.充气器.充气(R.layout.list_项,空);
TextView TextView=(TextView)row.findViewById(R.id.title);
ImageView ImageView=(ImageView)row.findViewById(R.id.icon);
Tag Tag=this.tags.get(位置);
setText(tag.getTitle());
setImageResource(R.drawable.icon);
返回行;
}
}
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 & 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>
<?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>
activity_tags.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 & 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>
<?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>
如何解决此问题?
this.setContentView(R.layout.activity\u main)代码>应该是this.setContentView(R.layout.activity_标记)代码>此.setContentView(R.layout.activity\u main)代码>应该是this.setContentView(R.layout.activity_标记)代码>由于“查看行”为空,因此出现此错误。只需在您的LazyLoader.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);
}
}
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;
}
}
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;
}
}
View row;
row = this.inflater.inflate(R.layout.list_item, null);
与:
由于“视图行”为空,因此出现此错误。只需在您的LazyLoader.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);
}
}
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;
}
}
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;
}
}
View row;
row = this.inflater.inflate(R.layout.list_item, null);
与:
谢谢,我想那是个错误,但我还是犯了同样的错误(谢谢,我想这是一个错误,但我仍然会遇到同样的错误。)(你可能不想只发布一个类,你可能想澄清为什么这会解决问题中提出的问题。这样海报可以从中学习,以及未来的用户。因为我的英语非常差=(与仅仅发布一个类不同,你可能想澄清为什么这可以解决问题中提出的问题。这样,海报可以从中学习,也可以向未来的用户学习。因为我的英语很差=(