Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我做错了什么。。Android编程新手_Java_Android - Fatal编程技术网

Java 我做错了什么。。Android编程新手

Java 我做错了什么。。Android编程新手,java,android,Java,Android,我正在尝试在Android中创建一个自定义的ListViewArrayAdapter,它显示我创建的名为Brick的类中的数据。我的主要活动调用一个返回ArrayList的方法。我不知道我做错了什么,LogCat和Debugger没有告诉我太多。我想这是我的错误,因为我是Android新手。当我启动ListActivity时,应用程序强制关闭。我已经尝试了多个教程,但进展很快 这是我的自定义适配器: import android.view.View; import android.view.Vi

我正在尝试在Android中创建一个自定义的
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;
}