您的内容必须具有 id 属性为 'android.R.id.list' 列表视图片段的列表视图



我的列表视图中有错误 您的内容必须具有 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.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>

和这个时间线片段.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;
}

和这个基本列表片段.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;
    }
}

注:已编辑

我已经检查了XML,这是真正的格式列表视图Android:id="@android:ID/list",但为什么仍然错误? 有人帮我吗? 对不起我的英语

尝试像这样做 sth

public class FragmentActivity extends ListFragment{
//your code.
}

从你的评论中你说片段扩展了ListFragment

因此,您需要在timeline.xml中具有 id @android:id/list的列表视图

因为你给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>

R.layout.not_logged_in必须包含 listview 和 @android:id/list<</p>

div class="one_answers">

使用 ListFragment.setListAdapter()。无需对列表视图执行查找视图。请参考此链接

最新更新