11

FragmentListViewを使用しようとしています。しかし、私はこの Error: FATAL EXCEPTION: main java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class を取得します。 私のコード:

マイリストフラグメント:

public class whitelist_list extends ListFragment {


    Context mContext;

    @Override
    public void onAttach(Activity activity) {
        mContext = activity;
        Log.i("Event", "onAttach called");
        super.onAttach(activity);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
        return inflater.inflate(R.layout.whitelist_content, container, false);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Do something with the data
    }

}


whitelist_list newFragment = new whitelist_list();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, newFragment)
.commit();

編集: 解決策: リストビューを android:id="@id/android:list" で宣言する

4

2 に答える 2

1

しばらく前にこのエラーが発生しました。何らかの理由で、レイアウトの XML が変更されたため、コンポーネントの 1 つに間違った型がありました。xml に移動し、その項目に属していない「android.R.id.list」を探します。どのように変更されたのかはわかりませんが、おそらく GUI エディターを使用しているときに、あるべきではないものをリストに変更したと思います。表示されない場合は、レイアウトを投稿してください

于 2013-07-27T15:18:56.100 に答える