【问题标题】:List is not getting created in R.java未在 R.java 中创建列表
【发布时间】: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


    【解决方案1】:

    你应该设置你的 ListView 的 id:

    android:id="@+id/list"
    

    + 符号表示这是要在应用程序的命名空间中定义的新资源,即添加到 R.java。你目前拥有的:

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

    ...指的是 Android 框架中的预定义 ID。

    【讨论】:

      【解决方案2】:

      有一种说法,如果你不能在 R.Java 中创建文件,就说明 XML 有问题。

      如果你查看你的列表视图,你有“android:id="@android:id/list",这应该是

      "@+id/your_idname".
      

      希望这能解决问题。

      如果它仍然不起作用,请让我!

      【讨论】:

        【解决方案3】:

        @ViewById 不是 RoboGuice 注释。看起来你正在混合你的图书馆。你想要@InjectView。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-09-05
          • 1970-01-01
          • 2013-05-11
          • 1970-01-01
          • 2012-12-12
          • 2013-01-28
          • 1970-01-01
          相关资源
          最近更新 更多