0

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?

Prasanna
  • 2,593
  • 7
  • 39
  • 53

3 Answers3

2

You should set the id of your ListView with:

android:id="@+id/list"

The + symbol indicates that this is a new resource to be defined in your application's namespace, i.e. added to R.java. What you currently have:

android:id="@android:id/list"

...refers to a pre-defined id within the Android framework.

NigelK
  • 8,255
  • 2
  • 30
  • 28
1

There is a saying that, if you can't create file in R.Java, it means there is a problem with XML.

If you look at your listview, you have "android:id="@android:id/list", this should have been

"@+id/your_idname".

Hopefully this should resolve the problem.

Let me if its still not working!!

Dushyant Patel
  • 687
  • 8
  • 20
0

@ViewById is not a RoboGuice annotation. Looks like you're mixing your libraries. You want @InjectView.

emmby
  • 99,783
  • 65
  • 191
  • 249