Android应用程序崩溃,ListActivity类,充气器问题,每行定制视图

时间:2013-10-19 17:11:51

标签: android

我的MainActivity扩展为ListActivity,而onCreate方法则遵循代码

setListAdapter(new MyAdapter(this,
    android.R.layout.simple_list_item_1,
    R.id.textView1,
    getResources().getStringArray(R.array.countries)));

此类在我的MainActivity范围内

public class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] strings) {
        super(context, resource, textViewResourceId, strings);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflator.inflate(R.layout.list_item, parent, true);

        TextView tv = (TextView)row.findViewById(R.id.textView1);
        ImageView iv = (ImageView)row.findViewById(R.id.imageView1);

        String[] items = getResources().getStringArray(R.array.countries);

        tv.setText(items[position]);
        iv.setImageResource(R.drawable.flag_1);


        return super.getView(position, convertView, parent);
    }

}

现在,当我运行此代码时,应用程序崩溃并出现警告错误

  

很遗憾,APPLICATION_NAME已停止

我觉得这一行View row = inflator.inflate(R.layout.list_item, parent, true);

有问题

PS:我对Android很新,并且不知道为什么会发生这种情况,LogCat太难读了,但这里有几行:

10-19 13:06:09.878: D/AndroidRuntime(1736): Shutting down VM
10-19 13:06:09.878: W/dalvikvm(1736): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-19 13:06:09.896: E/AndroidRuntime(1736): FATAL EXCEPTION: main
10-19 13:06:09.896: E/AndroidRuntime(1736): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.widget.AdapterView.addView(AdapterView.java:477)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.view.LayoutInflater.inflate(LayoutInflater.java:500)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at com.me.listexample.MainActivity$MyAdapter.getView(MainActivity.java:57)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.widget.AbsListView.obtainView(AbsListView.java:2177)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.widget.ListView.onMeasure(ListView.java:1159)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.view.View.measure(View.java:15848)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:494)

3 个答案:

答案 0 :(得分:1)

尝试这样,

public class MyAdapter extends ArrayAdapter<String> {
    private Context context;

        public MyAdapter(Context context, int resource, int textViewResourceId,
                String[] strings) {
            super(context, resource, textViewResourceId, strings);
            this.context=context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflator.inflate(R.layout.list_item,null);

            TextView tv = (TextView)row.findViewById(R.id.textView1);
            ImageView iv = (ImageView)row.findViewById(R.id.imageView1);

            String[] items = (Activity)context.getResources().getStringArray(R.array.countries);

            tv.setText(items[position]);
            iv.setImageResource(R.drawable.flag_1);


            return row;
        }

    }

答案 1 :(得分:0)

在构造函数中

 LayoutInflater inflator;
 String items[];
 public MyAdapter(Context context, int resource, int textViewResourceId,
        String[] strings) {
    super(context, resource, textViewResourceId, strings);
    inflator = LayoutInflate.from(context);
    this.items = strings;
}

getView

View view = inflator.inflate(R.layout.list_item, parent, false);
TextView tv = (TextView)row.findViewById(R.id.textView1);
ImageView iv = (ImageView)row.findViewById(R.id.imageView1);
tv.setText(items[position]);
iv.setImageResource(R.drawable.flag_1);
return view;  

删除

String[] items = getResources().getStringArray(R.array.countries);

您已将数组传递给适配器类

的结构函数

最好使用ViewHolder模式

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

编辑:

static class ViewHolder
{
TextView tv;
ImageView iv;
} 

在getView中

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    if(convertView==null)
    {
        holder = new ViewHolder();
        convertView = inflator.inflate(R.layout.list_item, null);
        holder.iv= (ImageView) convertView.findViewById(R.id.imageView1);
        holder.tv = (TextView) convertView.findViewById(R.id.textView1);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder)convertView.getTag();
    }
        holder.tv.setText(items[position]);
        holder.iv.setImageResource(R.drawable.flag_1);
    return convertView;
}

答案 2 :(得分:0)

试试这个

View row = inflator.inflate(R.layout.list_item, null, true);

而不是

View row = inflator.inflate(R.layout.list_item, parent, true);