When I try to set an adapter to list like list.setAdapter(adapter);
I get an exception because list is null at this line of execution in FromPageActivity.java. If my understanding is right, since I am using Roboguice, @ViewById should initialize that in 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);
}
StableArrayAdapter:
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>
What am I missing out?