使用两个片段,获取错误:内容具有id属性'android.R.id.list'的视图,该视图不是ListView类

时间:2014-04-18 12:36:48

标签: android listview android-listview android-listfragment

编辑(19/4)我附加了一个Dropbox链接,因为第一个错误已修复,但仍然是应用程序崩溃而没有理由,它从looper转到ZygotInit杀死应用程序,但我似乎无法找到原因,也没有logcat给出任何... https://www.dropbox.com/sh/2hrzqk9jnrlro0m/471jspyoCl

编辑: fragment_news_feed.xml:下面是有问题的...... ListFragment很长,因为它还包含asynctask,只附加覆盖:

View feedView;
ListView mRssFeed;
ArrayAdapter<String> rssListAdapter;
List<String> rssItemList;

public NewsFeed() {
}

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

    // Read in the flag indicating whether or not the user has demonstrated awareness of the
    // drawer. See PREF_USER_LEARNED_DRAWER for details.
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
    //mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);

    if (savedInstanceState != null) {
        //mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
        //mFromSavedInstanceState = true;
    }

    rssItemList = new ArrayList<>();
    rssItemList.add("T");
    rssListAdapter = new ArrayAdapter<>(getActivity(), R.layout.fragment_news_feed, R.id.item, rssItemList);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Indicate that this fragment would like to influence the set of actions in the action bar.
    setHasOptionsMenu(true);
    mRssFeed = getView().getListView();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    feedView = inflater.inflate(R.layout.fragment_news_feed, container, false);
    //mRssFeed = getView().getListView();
    /*String rss = "";
    try {
        rss = getRssFeed();
        rssItemList.clear();
        rssItemList.add(rss);
        rssListAdapter.add(rss);
        mRssFeed.setAdapter(rssListAdapter);
    }
    catch (Exception ex) {
        Log.w("My News", "Exception Caught: " + ex.toString());
    }*/
    return feedView;
}

@Override
public void onStart() {
    super.onStart();
    new GetRssFeedTask().execute();
}

ORIGINAL: 我正在写一个小应用程序,有两个片段(一个Fragment和一个ListFragment)和两个布局(其中一个在其中一个片段中)。

第一个片段是:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"   
    android:dividerHeight="0dp"
    android:background="#cccc"
    tools:context="com.kfir.bs.mynews.mynews.NavigationDrawerFragment" />

使用onCreateView工作正常!!

有问题的片段是布局:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.kfir.bs.mynews.mynews.NewsFeed$PlaceholderFragment">

    <ListView
        android:id="@+id/feedlist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</RelativeLayout>

第二个片段的onCreateView是:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        feedView = inflater.inflate(R.layout.fragment_news_feed, container, false);
        mRssFeed = (ListView) feedView.findViewById(R.id.feedlist);
        // TODO here I need to reed the RSS and inflate the list with data
        String rss = "";
        try {
            rss = getRssFeed();
            rssItemList.clear();
            rssItemList.add(rss);
            rssListAdapter.add(rss);
            mRssFeed.setAdapter(rssListAdapter);
        }
        catch (Exception ex) {
            Log.w("My News", "Exception Caught: " + ex.toString());
        }
        return feedView;
    }

我使用gradle与Android Studio合作。 构建像黄油一样顺畅,但是...当我运行应用程序时, 当它进入secong onCreateView(如上所示)时,应用程序崩溃了 runTime执行错误:

  

java.lang.RuntimeException:Content具有id属性的视图   &#39; android.R.id.list&#39;这不是ListView类

虽然我的布局被夸大了,并且列表得到了处理,但我仍然得到例外。

可能是什么问题?

非常感谢, ķ。

1 个答案:

答案 0 :(得分:2)

您需要更改此

android:id="@+id/feedlist"

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

在onActivityCreated

mRssFeed = getView().getListView();

或更改

extends ListFragment 

extends Fragment

保持其余部分相同。