简体   繁体   English

在将值从Activity传递给服务时获取空指针异常

[英]Getting null pointer exception while passing value from Activity to service

In Activity i have passed a value paramVal like this 在Activity中,我传递了一个像这样的值paramVal

 Intent srv = new Intent(this, TestService.class);
    startService(srv);

     //Intent intent =new Intent("com.example.firstapplication.STARTACTIVITY"); 
     //intent.putExtra("myFirstKey", paramVal); 

    //intent.putExtra("myFirstKey", paramVal); 

    Bundle bundle = new Bundle();
    bundle.putCharSequence("myFirstKey", paramVal);
    intent.putExtras(bundle);

and in service I am retrieving a value like this 在服务中我正在检索这样的值

 @Override
  public void onStart(Intent intent, int startId) {
      // TODO Auto-generated method stub
      super.onStart(intent, startId);

      Bundle bundle = intent.getExtras();
      data = (String) bundle.getCharSequence("myFirstKey");

      System.out.println("data checking"+data);
     }

but i am getting null pointer exception for the line 但我得到该行的空指针异常

data = (String) bundle.getCharSequence("myFirstKey");

in service 在服务中

should i have to call onstart method in service somewhere. 我应该在某个地方调用onstart方法吗? Please let me know where is the problem?? 请让我知道问题出在哪里?

In your Activity: 在您的活动中:

Intent lIntent = new Intent(this, TestService.class);
lIntent.putExtra("myFirstKey", <your String param here>);
startService(lIntent);

In your Service: 在您的服务中:

String lDataString = intent.getStringExtra("myFirstKey");

As a sidenote: 作为旁注:

  • please override onStartCommand() in your service, since onStart() is deprecated 请覆盖您服务中的onStartCommand(),因为不推荐使用onStart()
  • it is not required to call super.onStartCommand() (or super.onStart() for that matter) when extending the Service class 扩展Service类时不需要调用super.onStartCommand()(或者super.onStart())

你在传递额外内容之前就开始了这项服务......

It might work, you are changing the order of instruction. 它可能有效,你正在改变指令的顺序。 the service is starting before inserting values in the intent. 在intent中插入值之前,服务正在启动。

Intent srv = new Intent(this, TestService.class);
Bundle bundle = new Bundle();
    bundle.putCharSequence("myFirstKey", paramVal);
    intent.putExtras(bundle);
startService(srv);

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

相关问题 从片段向活动传递值时,空指针异常 - Null Pointer Exception when passing value from Fragment to Activity 服务未与活动绑定,空指针异常 - Service is not binded from activity, null pointer exception 将字符串的ArrayList从一个活动传递到另一个活动时,为什么会出现空指针异常? - Why am I getting a null pointer exception when passing an ArrayList of strings from one activity to another? 为什么在将数据从活动传递到片段时出现空指针异常? - Why do I get a null pointer exception while passing the data from activity to fragment? 从Android中的另一个活动获取图像时获取空指针异常 - Getting null pointer exception while getting image from another activity in android 将数据从活动传递到片段时出现空指针异常 - Null pointer exception when passing data from activity to fragment 空指针异常将数据从活动传递到片段 - Null Pointer exception passing data from activity to Fragment 从服务类转换为活动时出现空指针异常 - Null pointer exception when casting from a service class to an activity 从onMenuItemClick()方法调用活动时获取空指针异常 - Getting Null Pointer Exception when calling an Activity from onMenuItemClick() method 从不同的类调用活动时获取空指针异常 - Getting null pointer exception when calling activity from different class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM