Your content must have a ListView whose id attribute is ‘android.R.id.…

综合技术 2018-01-01

I have get Error in my listview Your content must have a ListView whose id attribute is '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)

this xml with name timeline.xml



    

        
    

    

and this 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 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 {
        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;
}

and this 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 statuses) {
        // Sub-classes should override this function to implement
        // functionality upon retrieval of data
        return;
    }
}

NB : EDITED

i have check the xml, this is true format listview android:id="@android:id/list
", but why still error ? anybody help me ? sorry for my english

try doing sth like this

public class FragmentActivity extends ListFragment{

//your code.

}

您可能感兴趣的

The Android button is on listview first, I want to say sorry but i have my problems with designing android app 😀 I want that the button is below the listview. Actually the button is o...
Android ListView Tutorial with Kotlin Update Note : This tutorial is now up to date with the latest version of Android Studio version 3.0.1, and uses Kotlin for app development. Updat...
win10 uwp 动态修改ListView元素布局 本文告诉大家如何在代码动态修改 ListView 的元素布局,从垂直修改为水平,从水平修改为垂直 先给大家一张图看一下效果 如何需要从 cs 代码修改 ListView 的 ItemsPanel 的 ItemsPanelTemplate 从而修改元素布局是比较困难的。如修改下...
Custom Earpiece button Click the ListView item I have created a Custom Listener interface for Button click in Adapter class, i followed this tutorial : http://www.c-sharpcorner.com/...
ListView does not call ItemCreated after postback I have an asp:ListView where I want to filter the items based on a property of the contained DataItem I want to set the item invisible unless the sp...