Java 我做错了什么。。Android编程新手
我正在尝试在Android中创建一个自定义的Java 我做错了什么。。Android编程新手,java,android,Java,Android,我正在尝试在Android中创建一个自定义的ListViewArrayAdapter,它显示我创建的名为Brick的类中的数据。我的主要活动调用一个返回ArrayList的方法。我不知道我做错了什么,LogCat和Debugger没有告诉我太多。我想这是我的错误,因为我是Android新手。当我启动ListActivity时,应用程序强制关闭。我已经尝试了多个教程,但进展很快 这是我的自定义适配器: import android.view.View; import android.view.Vi
ListView
ArrayAdapter,它显示我创建的名为Brick的类中的数据。我的主要活动调用一个返回ArrayList的方法。我不知道我做错了什么,LogCat和Debugger没有告诉我太多。我想这是我的错误,因为我是Android新手。当我启动ListActivity时,应用程序强制关闭。我已经尝试了多个教程,但进展很快
这是我的自定义适配器:
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class BrickListAdapter extends ArrayAdapter<Brick> {
private ArrayList<Brick> bricks;
private Context mContext;
public BrickListAdapter(Context context, int textViewResourceId, ArrayList<Brick> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.bricks = objects;
this.mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v != null){
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.bricklistitems, null);
Brick b = bricks.get(position);
if(b != null){
TextView tn = (TextView) v.findViewById(R.id.listitemname);
TextView td = (TextView) v.findViewById(R.id.listitemdesc);
if(tn !=null){
tn.setText(b.getName());
}
if(td != null){
td.setText(b.getWidth());
}
}
}
return v;
}
}
这可能是导致错误的原因:
td.setText(b.getWidth());
这可能是调用获取资源id的setText()。请改用它(获取CharSequence的):
这可能是导致错误的原因:
td.setText(b.getWidth());
这可能是调用获取资源id的setText()。请改用它(获取CharSequence的):
更改getView()
方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){// Changed Here
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.bricklistitems, null);
}
Brick b = bricks.get(position);
TextView tn = (TextView) v.findViewById(R.id.listitemname);
TextView td = (TextView) v.findViewById(R.id.listitemdesc);
if(tn !=null){
tn.setText(b.getName());
}
if(td != null){
td.setText(""+ b.getWidth());// Changed Here
}
return v;
}
更改getView()
方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){// Changed Here
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.bricklistitems, null);
}
Brick b = bricks.get(position);
TextView tn = (TextView) v.findViewById(R.id.listitemname);
TextView td = (TextView) v.findViewById(R.id.listitemdesc);
if(tn !=null){
tn.setText(b.getName());
}
if(td != null){
td.setText(""+ b.getWidth());// Changed Here
}
return v;
}
编辑您的问题并添加
logcat
error messages编辑您的问题并添加logcat
error messages非常感谢您是(v==null)行!非常感谢是(v==null)行!
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){// Changed Here
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.bricklistitems, null);
}
Brick b = bricks.get(position);
TextView tn = (TextView) v.findViewById(R.id.listitemname);
TextView td = (TextView) v.findViewById(R.id.listitemdesc);
if(tn !=null){
tn.setText(b.getName());
}
if(td != null){
td.setText(""+ b.getWidth());// Changed Here
}
return v;
}