Android ListView示例崩溃。无法启动应用

时间:2015-03-24 11:46:05

标签: android listview layout

我花了很多时间尝试应用我学到的东西,但每当我在手机上运行这个例子时,它就会崩溃。

我想要做的只是一个带有2个textview的ImageView,就像联系人在手机上看起来一样。

这是我的代码:

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

    Resources res = getResources();
    names = res.getStringArray(R.array.contacts);
    info = res.getStringArray(R.array.descript);
    int [] images = {R.drawable.pic0, R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4};

    list = (ListView) findViewById(R.id.listView);

    myAdapter adapter = new myAdapter(this,names,images,info);
    list.setAdapter(adapter);



}
class myAdapter extends ArrayAdapter<String> {
    Context context;
    int[] pictures;
    String[] conArray;
    String[] infoArray;
    myAdapter(Context c,String[] contacts, int images[],String[] descript){

        super(c,R.layout.single_row,R.id.textView,contacts);
        this.context = c;
        this.pictures=images;
        this.conArray=contacts;
        this.infoArray=descript;


    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        View v =inflater.inflate(R.layout.single_row,parent,false);

        ImageView image = (ImageView) findViewById(R.id.imageView);
        TextView name = (TextView) findViewById(R.id.textView);
        TextView des = (TextView) findViewById(R.id.textView2);

        image.setImageResource(pictures[position]);
        name.setText(conArray[position]);
        des.setText(infoArray[position]);

        return v;
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
    }
}

2 个答案:

答案 0 :(得分:2)

getView(....)

的变化
 ImageView image = (ImageView) findViewById(R.id.imageView);
 TextView name = (TextView) findViewById(R.id.textView);
 TextView des = (TextView) findViewById(R.id.textView2);

ImageView image = (ImageView) v.findViewById(R.id.imageView);
TextView name = (TextView) v.findViewById(R.id.textView);
TextView des = (TextView) v.findViewById(R.id.textView2);

从充气的rootview中找到View

并使用View Holder pattern来平滑滚动http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html

答案 1 :(得分:0)

检查数组长度是否相等

Resources res = getResources();
names = res.getStringArray(R.array.contacts);
info = res.getStringArray(R.array.descript);
int [] images = {R.drawable.pic0, R.drawable.pic1, R.drawable.pic2,R.drawable.pic3, R.drawable.pic4};

联系字符串数组和名称字符串数组应该等于 int []图像