我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)