【发布时间】:2012-02-26 01:23:46
【问题描述】:
最近我一直在玩 Intent 和 bundles。我以为我已经弄清楚了,但是在意图之间传递数据时我一直遇到问题我知道你必须使用捆绑包,但是当我尝试实现一个简单的程序来测试它时,我一直得到一个空指针异常。我制作的程序只是一个调用服务来创建字符串的活动,然后该活动应该能够获取服务创建的字符串并对其进行烘烤。有谁知道我在这里做错了什么,为任何可以提供帮助的人欢呼。下面是代码
活动类
MyIntent = new Intent(this, GetLocation.class);
startService(MyIntent);
bundle = MyIntent.getExtras();
test = bundle.getString("location");
Context context = getApplicationContext();
CharSequence text = test;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
服务类
Intent Int1 =new Intent(this,MapMock.class);
Bundle b = new Bundle();
String yeah = new String();
yeah = "hello";
b.putString("location", yeah);
Int1.putExtras(b);
【问题讨论】:
-
不清楚上述代码是如何或何时运行的,以及错误发生在哪里。
-
需要从Activity接收Service类中的数据
标签: android android-intent bundle