【发布时间】:2014-02-12 20:53:42
【问题描述】:
当我尝试将适配器设置为像list.setAdapter(adapter); 这样的列表时,我得到一个异常,因为在 FromPageActivity.java 中的这行执行中列表为空。如果我的理解是正确的,因为我使用的是 Roboguice,@ViewById 应该在 R.java 中初始化它。
FromPageActivity.java
@RoboGuice
@EActivity(R.layout.from_page)
public class FromPageActivity extends SherlockListActivity {
@ViewById ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
"Android", "iPhone", "WindowsMobile" };
final List<String> ls = (List<String>)Arrays.asList(values);
final StableArrayAdapter adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, (List<String>) ls);
list.setAdapter(adapter);
}
稳定阵列适配器:
public class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
from_page.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:text="@string/hello" />
</LinearLayout>
我错过了什么?
【问题讨论】:
标签: android actionbarsherlock r.java-file roboguice android-annotations