您的内容必须具有ListView,其id属性为'android.R.id.list'listview Fragment

时间:2014-03-04 11:00:54

标签: android listview

我的listview中出现错误您的内容必须有一个ListView,其id属性为'android.R.id.list'

 03-04 17:53:12.558: E/AndroidRuntime(8469): FATAL EXCEPTION: main
    03-04 17:53:12.558: E/AndroidRuntime(8469): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
    03-04 17:53:12.558: E/AndroidRuntime(8469):     at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
    03-04 17:53:12.558: E/AndroidRuntime(8469):     at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
    03-04 17:53:12.558: E/AndroidRuntime(8469):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
    03-04 17:53:12.558: E/AndroidRuntime(8469):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1105)
    03-04 17:53:12.558: E/AndroidRuntime(8469):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    03-04 17:53:12.558: E/AndroidRuntime(8469):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)
    03-04 17:53:12.558: E/AndroidRuntime(8469):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
    03-04 17:53:12.558: E/AndroidRuntime(8469):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
    03-04 17:53:12.558: E/AndroidRuntime(8469):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)

此xml名称为 timeline.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#DDD"
        android:gravity="center"
        android:orientation="horizontal" >

        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />
    </LinearLayout>

    <ListView

    android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:divider="#DDD"
        android:dividerHeight="1dp"
        android:fadingEdge="none" />

</LinearLayout>

TimelineFragment.class

package app.jrupac.cleantwitter;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;

import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.User;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class TimelineFragment extends BaseListFragment {

    public final String TAG = Utils.TAG_BASE + this.getClass().getName();

    private static TimelineFragment mTimelineFragment = null;

    private View mView;
    private ListView mListView;
    private TweetData[] mUpdatedTweets = null;
    private TweetAdapter mAdapter = null;
    private Context mContext;
    private OAuth mOAuth;
    private TwitterAPI mTwitterAPI;

    public static TimelineFragment getInstance() {
        if (mTimelineFragment == null) {
            mTimelineFragment = new TimelineFragment();
        }

        return mTimelineFragment;
    }

    @Override
    public void onForceRefresh() {
         Log.i(TAG, "Getting updates for timeline");
         mTwitterAPI.getHomeTimeline(this);
    }

    @Override
    public void onParseCompleted(ResponseList<Status> statuses) {
        mUpdatedTweets = new TweetData[statuses.size()];
        for (int i = 0; i < mUpdatedTweets.length; i++) {
            TweetData t = new TweetData();
            Status s = statuses.get(i);
            User u = s.getUser();

            t.name = u.getName();
            t.username = "@" + u.getScreenName();
            t.text = s.getText();
            t.time = u.getCreatedAt();
            try {
                t.avatar_url = new URL(u.getProfileImageURL());
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            t.avatar = null;

            mUpdatedTweets[i] = t;
        }

        postResults(false);
    }

    private void postResults(boolean getFromDB) {
        mAdapter = new TweetAdapter(mContext, R.layout.timeline_listitem,
                mUpdatedTweets);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.timeline, container, false);
        mListView = (ListView) mView.findViewById(android.R.id.list);
        mListView.setEmptyView(mView.findViewById(android.R.id.empty));
        mContext = getActivity().getApplicationContext();
        mOAuth = OAuth.getInstance((BaseActivity) getActivity());
        mTwitterAPI = TwitterAPI.getInstance(mContext);

        if (mOAuth.isLoggedIn()) {
            Log.i(TAG, "Getting updates for timeline");
            mTwitterAPI.getHomeTimeline(this);
            return mView;
        } else {
            return inflater.inflate(R.layout.not_logged_in, container, false);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private class TweetAdapter extends ArrayAdapter<TweetData> {
        private TweetData[] mTweets;
        private LayoutInflater mInflater;
        private View mView;
        private SimpleDateFormat mSdf;
        private final String DATE_FORMAT = "hh:mm aa";

        public TweetAdapter(Context context, int textViewResourceId,
                TweetData[] objects) {
            super(context, textViewResourceId, objects);
            mTweets = objects;
            mInflater = LayoutInflater.from(context);
            mSdf = new SimpleDateFormat(DATE_FORMAT);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView == null) {
                mView = mInflater.inflate(R.layout.timeline_listitem, null);

                holder = new ViewHolder();
                holder.name = (TextView) mView.findViewById(R.id.tweet_name);
                holder.username = (TextView) mView
                        .findViewById(R.id.tweet_username);
                holder.time = (TextView) mView.findViewById(R.id.tweet_time);
                holder.text = (TextView) mView.findViewById(R.id.tweet_text);
                holder.avatar = (ImageView) mView
                        .findViewById(R.id.tweet_avatar);
                mView.setTag(holder);
            } else {
                mView = convertView;
                holder = (ViewHolder) mView.getTag();
            }

            TweetData current = mTweets[position];

            holder.name.setText(Html.fromHtml(current.name));
            holder.username.setText(Html.fromHtml(current.username));
            holder.time.setText(mSdf.format(current.time));
            holder.text.setText(Html.fromHtml(current.text));

            if (current.avatar == null) {
                ThumbnailDownloader.fetchDrawable(current, holder.avatar);
            } else {
                holder.avatar.setImageDrawable(current.avatar);
            }

            return mView;
        }
    }
}

class ViewHolder {
    public TextView name;
    public TextView username;
    public TextView time;
    public TextView text;
    public ImageView avatar;
}

BaseListFragment.class

package app.jrupac.cleantwitter;

import twitter4j.ResponseList;
import twitter4j.Status;
import android.graphics.Bitmap;
import android.support.v4.app.ListFragment;
import android.view.View;

public class BaseListFragment extends ListFragment {

    public void onForceRefresh() {
        return;
    }

    public void onThumbnailDownload(Bitmap bmp, View v) {
        // Sub-classes should override this function to implement
        // functionality upon retrieval of images
        return;
    }

    public void onParseCompleted(ResponseList<Status> statuses) {
        // Sub-classes should override this function to implement
        // functionality upon retrieval of data
        return;
    }
}

NB:已编辑

我已经检查了xml,这是真正的格式listview android:id =“@ android:id / list ”,但为什么还有错误?有人帮帮我吗?对不起我的英文

4 个答案:

答案 0 :(得分:0)

尝试做这样的事情

public class FragmentActivity extends ListFragment{

//your code.

}

答案 1 :(得分:0)

根据你的评论,你说片段扩展了ListFragment

因此,您需要在@android:id/list

中包含ID为timeline.xml的列表视图

因为你膨胀了timeline.xml

 mView = inflater.inflate(R.layout.timeline, container, false);

更多信息@

http://developer.android.com/reference/android/app/ListActivity.html

适用于ListAcitivity的内容适用于ListFragment

根据您发布的内容,ListView中的main.xml不在timeline.xml

编辑:

timeline.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/ll"
       >

        <EditText
            android:id="@+id/edit_message"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="sendMessage" />
    </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_below="@id/ll"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:divider="#DDD"
        android:dividerHeight="1dp"
        android:fadingEdge="none" />

</RelativeLayout>

答案 2 :(得分:0)

R.layout.not_logged_in必须包含带有@android:list / list

的listview

答案 3 :(得分:-1)

使用ListFragment.setListAdapter()。无需为列表视图执行findviewby id。 请参阅此link