【问题标题】:Broadcasting a list using sendBroadcast()使用 sendBroadcast() 广播列表
【发布时间】:2020-06-09 00:24:16
【问题描述】:

我有一个列表,我正在尝试使用意图进行广播。遵循在线教程后,有人建议我使用 Parcelable 来发送此数据。但是,我在 logcat 中不断收到此错误:

Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to android.os.Parcelable

从这行代码 bundle.putParcelable("data", (Parcelable)tweets); 我不知道如何纠正这个问题。

我在哪里构建意图

    protected void onHandleWork(@NonNull Intent intent) {
        Log.d(TAG, "onHandleWork: ");
        List<tweet> tweets = new ArrayList();

        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer("HyjgZgfiqSODTdICZUXIHI8HK", "TlynMItosq99QxnLMLGxA6FElD3TAKx9UmBxva5oExg9Gz1mzV");
        AccessToken accessToken = new AccessToken("2362719277-w5QlRNB2I7PXdMJuDXf5cc8FDT5H8X38ujxrtiT", "3v2Z2cqezaFrV6pFHu2yfPVFHZgMvLjMVKH4cUujI9kwI");
        twitter.setOAuthAccessToken(accessToken);
        Query query = new Query("Twitch");
        try {
            QueryResult result = twitter.search(query);
            for (Status status : result.getTweets()) {
                String createdat = status.getCreatedAt().toString();
                String text = status.getText();
                String retweets = String.valueOf(status.getRetweetCount());
                String favs = String.valueOf(status.getFavoriteCount());
                String uri = status.getUser().getProfileImageURL();
               tweet onetweet = new tweet(createdat,text,retweets,favs,uri);

              //  Log.d(TAG, status.getText());
                tweets.add(onetweet);
            }
            if (isStopped()) return;
        } catch (TwitterException e) {
            e.printStackTrace();
        }
        sendToUI(tweets);

    }

    private void sendToUI(List tweets) {
        Intent intent = new Intent("tweet_result");
        Bundle bundle = new Bundle();
        bundle.putParcelable("data", tweets);
        intent.putExtras(bundle);
        sendBroadcast(intent);
    }

我的推文 POJO

import android.os.Parcel;
import android.os.Parcelable;

public class tweet implements Parcelable {
    private String created_at;
    private String text;
    private String retweet_count;
    private String favorite_count;
    private String image_uri;

    public String getImage_uri() {
        return image_uri;
    }


    public String getCreated_at() {
        return created_at;
    }

    public String getText() {
        return text;
    }

    public String getRetweet_count() {
        return retweet_count;
    }

    public String getFavorite_count() {
        return favorite_count;
    }


    protected tweet(Parcel in) {
        created_at = in.readString();
        text = in.readString();
        retweet_count = in.readString();
        favorite_count = in.readString();
        image_uri = in.readString();
    }

    public static final Creator<tweet> CREATOR = new Creator<tweet>() {
        @Override
        public tweet createFromParcel(Parcel in) {
            return new tweet(in);
        }

        @Override
        public tweet[] newArray(int size) {
            return new tweet[size];
        }
    };


    @Override
    public int describeContents() {
        return 0;
    }

    public tweet(String created_at, String text, String retweet_count, String favorite_count, String image_uri) {
        this.created_at = created_at;
        this.text = text;
        this.retweet_count = retweet_count;
        this.favorite_count = favorite_count;
        this.image_uri = image_uri;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(created_at);
        dest.writeString(text);
        dest.writeString(retweet_count);
        dest.writeString(favorite_count);
        dest.writeString(image_uri);
    }
}

【问题讨论】:

    标签: java android arraylist parcelable


    【解决方案1】:

    你使用了错误的方法,你应该使用intent.putParcelableArrayListExtra(),但不要忘记你的数组列表必须只包含parcelables项目。

    【讨论】:

    • 首先,当我进行更改时,我现在收到此错误。 putParcelableArrayListExtra cannot be applied to (android.os.bundle) 其次,我如何确保我的arraylist 只包含你所说的parcelables?
    • 你得到错误,因为你把你的列表放入一个包中,如果你使用 putParcelableArrayListExtra 方法是错误的,因为该方法等待一个可包裹的数组列表,而不是一个包。至于您的第二个问题,一切都很好,您的 arrayList 仅包含实现 Parcelable 的 Tweet 类的实例。
    【解决方案2】:

    将我的 sendToUI() 更改为此有效:

        private void sendToUI(List tweets) {
            Intent intent = new Intent("tweet_result"); //tweet_result is a string to identify this intent
            Bundle bundle = new Bundle();
            bundle.putParcelableArrayList("data", (ArrayList<? extends Parcelable>) tweets);
            intent.putExtras(bundle);
            sendBroadcast(intent);
        }
    

    【讨论】:

    • 在这种情况下使用捆绑包是没用的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 2019-07-10
    相关资源
    最近更新 更多