使用intent将数据从服务发送到活动时出现空指针异常

时间:2017-01-02 07:29:34

标签: java android

我使用Bundle将数据从Service Class发送到Main Activity但是发送null。

服务类

{
  "supports": {},
  "dependencies": {
    "Chance.MvvmCross.Plugins.UserInteraction": "1.1.4",
    "Fody": "1.29.4",
    "MethodDecorator.Fody": "0.9.0.6",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "MvvmCross": "4.3.0",
    "MvvmCross.Platform": "4.3.0",
    "NEST": "5.0.0",
    "NETStandard.Library": "1.6.1",
    "Newtonsoft.Json": "9.0.1",
    "PropertyChanged.Fody": "1.52.1",
    "System.Linq": "4.3.0",
    "System.Linq.Expressions": "4.3.0",
    "System.Linq.Queryable": "4.3.0",
    "System.ServiceModel": "1.0.0",
    "System.ServiceModel.Http": "4.3.0",
    "System.ServiceModel.Security": "4.3.0"
  },
  "frameworks": {
    "netstandard1.5": {
      "imports": "portable-net45+netcore45+wp8"
    }
  }
}

主要活动

Intent intent = new Intent(LocationService.this, MainActivity.class);
Bundle b = new Bundle();
b.putString("final_address", addressText);
intent.putExtras(b);
startActivity(intent);

我也试过通过广播信息发送它但它也失败了。见下文:

服务类

Intent intentReceived = getIntent();
Bundle b = intentReceived.getExtras();
address = b.getString("final_address");

主要活动

public static final String ADDRESSIDENTIFY = "address_identify";
public static final String ADDRESS_PATH = "address_path";

Intent intent = new Intent(ADDRESSIDENTIFY);
intent.putExtra(ADDRESS_PATH, addressText);
sendBroadcast(intent);

并在创建

的主要活动中调用此方法
registerReceiver(receiver, new IntentFilter(LocationService.ADDRESSIDENTIFY));

我也在清单中声明了服务。

private BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            address = bundle.getString(LocationService.ADDRESS_PATH);

        }
    }
};

我该怎么办......我错在哪里?

2 个答案:

答案 0 :(得分:0)

你的代码是完美的,它在我的情况下工作。如果你遗失了什么,请与我核实。您是否在清单中定义了applicationpublic class LocationService extends Service { public static final String ADDRESSIDENTIFY = "address_identify"; public static final String ADDRESS_PATH = "address_path"; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.e("service", "oncreate"); Intent intent = new Intent(ADDRESSIDENTIFY); intent.putExtra(ADDRESS_PATH, "hello"); sendBroadcast(intent); } }

服务类

public class MainActivity extends AppCompatActivity {

    String address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        registerReceiver(receiver, new IntentFilter(LocationService.ADDRESSIDENTIFY));

        startService(new Intent(this, LocationService.class));
    }


    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                address = bundle.getString(LocationService.ADDRESS_PATH);
                System.out.println("val"+ " = "+ address);
            }
        }
    };
}

<强>活动

 ...
    <service android:name=".LocationService"
             android:enabled="true"/>

</application>

mainfest

db.runCommand({
    mapReduce: "waterColorModal",
    map: function() {
        for (var i = 0; i < this.tempArray.length; i++) {
          emit(new Date(this.dateField.toString()).getFullYear(), parseFloat(this.tempArray[i]));
        }

    },reduce: function(yearKey, totalValue) {
        count = 0;
        for (var index = 0; index < totalValue.length; ++index) {
            count += totalValue[index];
        }
        return count;
    },
    out: {
        inline: 1
    }
})

答案 1 :(得分:0)

无需发送广播使用LocalBroadcastManager,如下所示:

private LocalBroadcastManager mBroadcastManger;

将其放在servcie的onCreate()

mBroadcastManger = LocalBroadcastManager.getInstance(this);

使用以下意图发送广播:

Intent intent = new Intent(ADDRESSIDENTIFY);
intent.putExtra(ADDRESS_PATH, addressText);
mBroadcastManger.sendBroadcast(mIntent);