【问题标题】:SharedPreferances are giving null values in next activitySharedPreferences 在下一个活动中给出空值
【发布时间】:2017-11-27 05:20:45
【问题描述】:

我正在尝试将数据从活动发送到 BroadcastReceiver 类,然后再从接收器类发送到服务类。第一次数据正常,但之后它给出 nullPointer 异常

这是我的活动代码:-

  public void onClick(View view) {
                            Intent i=new Intent();
                            i.setClass(MainActivity.this, Myreceiver.class);
                            i.putExtra("name",et.getText().toString().trim());
                            sendBroadcast(i);
                            et.setText("");

                    }

在 Myreceiver 类中

 public void onReceive(Context context, Intent intent) {
    String data=intent.getExtras().getString("name");
    SharedPreferences sp=context.getSharedPreferences("MyPrefs",Context.MODE_PRIVATE);
    SharedPreferences.Editor editor=sp.edit();

    Intent i=new Intent(context,MyService.class);
    if (!data.equals("") && !data.isEmpty())
    {
        editor.putString("data",data).commit();
        i.putExtra("names",data);
        Log.e("Myreceiver data is ",data);

    }
    else {
        String name = sp.getString("data", "");
        i.putExtra("names",name);
        Log.e("Myreceiver data is not",name);
    }

        context.startService(i);




}

在我的服务类中

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {
     name=intent.getExtras().getString("names");\\Line 40
     Log.e("MyService class",name);

通常当活动的接收者在后台时,它第二次在上面的行给出空指针

错误日志:

  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                                                                           at com.example.evoqis.trnode.MyService.onStartCommand(MyService.java:40)

谢谢:)

【问题讨论】:

  • 你从哪里得到空指针?
  • @naveen 在服务类中,我使用意图获取名称。 Getextras()
  • @mitesh.. 我用过。提交()
  • 你的targetSdkVersion 是什么?并发布错误日志。
  • 已发布错误日志

标签: android service broadcastreceiver sharedpreferences


【解决方案1】:

在 handleIntent 和 onStartCommand 上使用此代码获取意图。

protected void onStartCommand (Intent intent, int flags, int startId) {
    data=(String) intent.getExtras().get("data"); 
}

【讨论】:

  • 但是已经有意图对象了。所以 getIntent() 在该方法中不可用,请参阅我的代码。
【解决方案2】:

您正在以捆绑包的形式接收数据,

intent.getExtras().getString("name"); //using getExtras() receives bundle 

但是您发送的数据没有捆绑,

i.putExtra("name",et.getText().toString().trim());

添加额外内容而不是额外内容。喜欢-

Bundle bundle = new Bundle();
bundle.putString("name", et.getText().toString().trim());
i.putExtras(bundle);

【讨论】:

    【解决方案3】:

    将 getExtras() 改为 getStringExtra()。

    MyReceiver 类

    public void onReceive(Context context, Intent intent) 
    {
     String data=intent.getStringExtra("name");
     ...
    }
    

    MyService 类

    public int onStartCommand(Intent intent, int flags, int startId) 
    {
     name=intent.getStringExtra("names");
     ...
    }
    

    【讨论】:

      【解决方案4】:

      感谢您的回复,但我为我的问题找到了一个非常简单的解决方案。将来可能会对某人有所帮助。 这是我的主要活动代码:

       public void onClick(View view) {
                                  Intent i=new Intent(MainActivity.this,Myreceiver.class);
                                  SharedPreferences sp=getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
                                  SharedPreferences.Editor editor=sp.edit();
                                  editor.putString("data",et.getText().toString()).commit();
                                  editor.apply();
                                  sendBroadcast(i);
                                  et.setText("");
      

      在服务类中

      SharedPreferences prefs = getSharedPreferences("MyPrefs",MODE_PRIVATE);
          String string = prefs.getString("data","");
      

      即使应用程序关闭,这也可以工作... 谢谢:)

      【讨论】:

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