【问题标题】:Android sending parcelable with setonclickfillinintentAndroid 使用 setonclickfillinintent 发送 Parcelable
【发布时间】:2015-07-12 21:35:15
【问题描述】:

我有一个带有列表视图的小部件。每当用户从列表视图中按下一个项目时,我都想接收在 Widget 列表视图的适配器中编译的 Parcelable 对象。

到目前为止一切正常,除了我无法接收 Parcelable 对象(其他一切正常)

适配器:

@Override
public RemoteViews getViewAt(int position) {
    ...
    Intent fillIntent = new Intent();
    fillIntent.putExtra("info", info); //info is a parcelable object
    fillIntent.setData(Uri.parse(fillIntent.toUri(Intent.URI_INTENT_SCHEME)));
    listViewItem.setOnClickFillInIntent(R.id.widget_listview_item, fillIntent);
    ...
}

提供者:

@Override
public void onReceive(Context context, Intent intent){
    ...
    else if(intent.getAction().equals(ACTION_CLICK_LISTITEM)){
        Info info = intent.getParcelableExtra("info");
        // show details
        if(info != null){
            Intent startIntent = new Intent(context, DetailActivity.class);
            startIntent.putExtra("info", info);
            startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startIntent.setData(Uri.parse(startIntent.toUri(Intent.URI_INTENT_SCHEME)));
            context.startActivity(startIntent);
        }else{
            Log.i(Constants.LOG_TAG, "info is null.");
        }
    }
    ...
}

我总是记录“信息为空。”...

注意:将 Info 类型的对象作为 parcelables 发送到其他活动在我的应用程序中工作得很好。我只在发送 parcelable 对象和 setOnClickFillInIntent 时遇到问题。

任何帮助表示赞赏。

【问题讨论】:

  • 检查您的 logcat 并发布您看到的任何错误。

标签: android listview android-intent android-listview parcelable


【解决方案1】:

据我所知,这个问题是由系统进程解析 Intent extras 引起的,系统进程没有适当的解析器来解析自定义 Parcelable 对象,请参阅 cmets 了解此问题:https://code.google.com/p/android/issues/detail?id=6822 了解详情

作为一种解决方法,您可以按照 cmets 中的建议将 Parcelable 包装在额外的 Bundle 对象中:

Bundle hackBundle = new Bundle();
hackBundle.put("key", myParcelable);
intent.putExtra("bundleKey", hackBundle);

它解决了我的问题

【讨论】:

    猜你喜欢
    • 2012-02-24
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多