发送:
Intent intent=new Intent(TestActivity2.this,
BroadcastReceiverGPS.class);
intent.setAction("com.xyz");
// 传输对象测试
SvStatus svStatus=new SvStatus();
svStatus.setB(1);
svStatus.setB1(1);
svStatus.setB2(1);
svStatus.setC(1f);
svStatus.setC(1f);
intent.putExtra("sv",svStatus);
sendBroadcast(intent);
接收
public void onReceive(Context context, Intent intent) {
String acion=intent.getAction().toString();
switch (acion){
case ACTION1:
Log.i(TAG,"reciver the abc...");
break;
case ACTION2:
Log.i(TAG,"reciver the xyz...");
// Object o= (intent.getSerializableExtra("sv"));
// SvStatus svStatus=(SvStatus)o;
// Log.i(TAG,"sv:"+svStatus.getB());
SvStatus s=intent.getParcelableExtra("sv");
Log.i(TAG,"sv:"+s.getB());
break;
default:
}
}
报错:
java.lang.RuntimeException: Unable to start receiver com.example.myapplication.broadcast.BroadcastReceiverGPS: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.myapplication.io.SvStatus.getB()' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:4041)
at android.app.ActivityThread.access$1400(ActivityThread.java:237)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1924)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.myapplication.io.SvStatus.getB()' on a null object reference
at com.example.myapplication.broadcast.BroadcastReceiverGPS.onReceive(BroadcastReceiverGPS.java:32)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:4032)