【问题标题】:passing data with intents有意图地传递数据
【发布时间】: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);

【问题讨论】:

标签: android android-intent bundle


【解决方案1】:

服务不能这样工作。

您通过 startService 发送到服务的意图不会更新并返回到启动它的活动。

我认为您需要绑定服务。 阅读here 了解更多信息。这允许您调用函数并将值传回活动。

【讨论】:

  • 谢谢 mcnicholls 我不知道绑定服务这正是我需要的东西,再次感谢
【解决方案2】:

看起来您正在访问您尚未输入的意图日期,

如果你从你的服务开始活动,并传递一些数据使用:

Intent myIntent=getIntent();
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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    相关资源
    最近更新 更多