简体   繁体   English

Bundle在传递给子活动时变为null

[英]Bundle becomes null when passed to a sub activity

I'm using the standard Android Intent/Bundle method to pass data to a sub activity. 我正在使用标准的Android Intent / Bundle方法将数据传递给子活动。

The problem is that although the Intent looks good before the activity is kicked off, it shows up as null in the sub activity. 问题是尽管在活动开始之前Intent看起来很好,但它在子活动中显示为null。 I can't find anyone else who has had this problem, so I thought I'd ask for pointers about where to look/how to debug. 我找不到其他有这个问题的人,所以我想我会问一下关于在哪里看/如何调试的指针。

(Target is Android 2.1) (目标是Android 2.1)

Parent activity snippet: 父活动代码段:

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Intent i = new Intent(ctx, ArticleView.class);

        i.putExtra("url", articles.get(position).url.toString());
        // The following line correctly logs the URL
        Log.d(TAG,"Starting article viewer with url " + i.getStringExtra("url"));

        startActivityForResult(i,ACTIVITY_ARTICLE);
    }

Child activity snippet: 子活动片段:

@Override
protected void onCreate(Bundle icicle) {

    super.onCreate(icicle);
    setContentView(R.layout.article_view);

    if (icicle != null) {
        // Never called!
    } else {
        // Always called
        Log.d(TAG,"Icicle is null - no URL passed in");
    }

}

Ignoring the fact that icicle is null and attempting to retrieve state information from it (with getString) results in a hang and: 忽略icicle为null并尝试从中检索状态信息(使用getString)的事实导致挂起并且:

03-14 22:23:03.529: WARN/ActivityManager(52): Activity HistoryRecord{44ddd238 com.t.s/.ArticlesList} being finished, but not in LRU list

Any hints greatly appreciated. 任何提示都非常感谢。

It's not the Intent that is passed to the constructor of the subactivity (or any activity). 它不是传递给子活动(或任何活动)的构造函数的Intent。

The intent can be fetched through 意图可以通过

Intent intent = getIntent();

The Bundle icicle you are referring to is the Bundle which the Activity uses when it is brought back from a paused state. 你所指的Bundle冰柱是当Activity从暂停状态恢复时使用的Bundle。 Eg if you need to save any state of your activity before it is restarted, this is the place where the stored data is. 例如,如果您需要在重新启动之前保存活动的任何状态,那么这就是存储数据所在的位置。

在OnCreate()中尝试以这种方式访问​​Bundle

Bundle b = getIntent().getExtras();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM