[英]int/string value becomes “0”/null when passed from one activity to another
[英]Bundle becomes null when passed to a sub activity
我正在使用标准的Android Intent / Bundle方法将数据传递给子活动。
问题是尽管在活动开始之前Intent看起来很好,但它在子活动中显示为null。 我找不到其他有这个问题的人,所以我想我会问一下关于在哪里看/如何调试的指针。
(目标是Android 2.1)
父活动代码段:
@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);
}
子活动片段:
@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");
}
}
忽略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
任何提示都非常感谢。
它不是传递给子活动(或任何活动)的构造函数的Intent。
意图可以通过
Intent intent = getIntent();
你所指的Bundle冰柱是当Activity从暂停状态恢复时使用的Bundle。 例如,如果您需要在重新启动之前保存活动的任何状态,那么这就是存储数据所在的位置。
在OnCreate()中尝试以这种方式访问Bundle
Bundle b = getIntent().getExtras();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.