我为ListView创建了自己的适配器。 ListView的每一行都由两个TextVews表示。我需要在ListView中显示一些信息。
这是我的活动代码:
public class MyCountriesActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int row_ID = R.layout.list_item;
ListAdapter adapt = new MyAdapter(this,row_ID);
setListAdapter(adapt);
}
}
这是我的适配器代码:
public class MyAdapter extends ArrayAdapter<String> {
Activity a;
int row_ID;
String[] countries = { "USA", "France" };
String[] years = { "1992", "2010" };
public MyAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
a = (Activity) context;
row_ID = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
if (convertView == null) { // Create new row view
LayoutInflater inflater = a.getLayoutInflater();
row = inflater.inflate(row_ID, parent, false);
} else
// reuse old row view to save time/battery
row = convertView;
/* Add new data to row object */
TextView country = (TextView) row.findViewById(R.id.country);
TextView year = (TextView) row.findViewById(R.id.year);
country.setText(countries[position]);
year.setText(years[position]);
return row;
}
}
但遗憾的是这个应用程序崩溃了。我的课程有什么问题?
日志:
03-04 17:41:11.044: E/AndroidRuntime(1277): FATAL EXCEPTION: main
03-04 17:41:11.044: E/AndroidRuntime(1277): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.ledinov.namespace/android.ledinov.namespace.MyCountriesActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-04 17:41:11.044: E/AndroidRuntime(1277): at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 17:41:11.044: E/AndroidRuntime(1277): at android.os.Looper.loop(Looper.java:137)
03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-04 17:41:11.044: E/AndroidRuntime(1277): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 17:41:11.044: E/AndroidRuntime(1277): at java.lang.reflect.Method.invoke(Method.java:511)
03-04 17:41:11.044: E/AndroidRuntime(1277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-04 17:41:11.044: E/AndroidRuntime(1277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-04 17:41:11.044: E/AndroidRuntime(1277): at dalvik.system.NativeStart.main(Native Method)
03-04 17:41:11.044: E/AndroidRuntime(1277): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
03-04 17:41:11.044: E/AndroidRuntime(1277): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.Activity.setContentView(Activity.java:1835)
03-04 17:41:11.044: E/AndroidRuntime(1277): at android.ledinov.namespace.MyCountriesActivity.onCreate(MyCountriesActivity.java:21)
03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.Activity.performCreate(Activity.java:4465)
03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-04 17:41:11.044: E/AndroidRuntime(1277): ... 11 more
main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
答案 0 :(得分:1)
如果我猜测您在ListView
布局文件或行布局文件@android:id/list
中没有R.layout.main
元素且标识为R.layout.list_item
,可能不是由单个Textview
元素组成,ArrayAdapter
在这种情况下会因ClassCastException
而崩溃,因为它需要一个TextView
元素。
此外,您可能希望将此方法添加到适配器:
public void getCount() {
return countries.length;
}
否则,即使您的应用运行,您也不会在ListView
中看到任何元素。如果TextViews
中有两个R.layout.list_item
,那就错了。像这样修改适配器:
public MyAdapter(Context context, int textViewResourceId, int textId) {
super(context, textViewResourceId, textId);
a = (Activity) context;
row_ID = textViewResourceId;
}
以及实例化适配器的位置:
int row_ID = R.layout.list_item;
int theId = R.id.the_id_of_one_of_the_textview_fromthelayout_above; (the id of the TextView from the R.layout.list_item)
ListAdapter adapt = new MyAdapter(this,row_ID, theId);
答案 1 :(得分:0)
以这种方式试试
public class MyAdapter extends ArrayAdapter<String> {
Activity a;
int row_ID;
String[] countries = { "USA", "France" };
String[] years = { "1992", "2010" };
public MyAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
this.context = context;
row_ID = textViewResourceId;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
Holder ho = null;
if (row == null )
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(row_ID, parent, false);
ho = new Holder();
txtCountry = (TextView) row.findViewById(R.id.country);
txtYear = (TextView) row.findViewById(R.id.year);
row.setTag(ho);
}
else{
ho = (Holder)row.getTag();
}
txtCountry.setText(countries[position]);
txtYear.setText(years[position]);
return row;
}
static class Holder
{
TextView txtCountry;
TextView txtYear;
}
}