我有2个应用程序,一个用于驱动程序,一个用于客户,当客户向驱动程序应用程序发送请求时,它被压碎了。 客户成功发送请求,但驾驶员无法接到客户的电话
错误说
FATAL EXCEPTION: Firebase-MyfirebaseMessaging
Process: com.ddu.dridedriver, PID: 14586
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference
at com.ddu.dridedriver.Service.MyfirebaseMessaging.onMessageReceived(MyfirebaseMessaging.java:19)
at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source:60)
at com.google.firebase.iid.zzg.run(Unknown Source:4)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source:6)
at java.lang.Thread.run(Thread.java:764)
MyfirebaseMessaging这是我的代码
public class MyfirebaseMessaging extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//coz it will be sent to firebase with lat and lng form customer application
LatLng customer_location = new Gson().fromJson(remoteMessage.getNotification().getBody(),LatLng.class);
Intent intent = new Intent(getBaseContext(),CustomerCall.class);
intent.putExtra("lat",customer_location.latitude);
intent.putExtra("lng",customer_location.longitude);
startActivity(intent);
}
}
``
在驱动程序项目中找到的我的customerCall类
public class CustomerCall extends AppCompatActivity {
TextView txtTime,txtAddress,txtDistance;
MediaPlayer mediaPlayer ;
IGoogleAPI mService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_call);
mService = Common.getGoogleApi();
txtAddress = (TextView)findViewById(R.id.txtAddress);
txtDistance = (TextView)findViewById(R.id.txtDistance);
txtTime = (TextView)findViewById(R.id.txtTime);
mediaPlayer = mediaPlayer.create(this,R.raw.ringtone);
mediaPlayer.setLooping(true);
mediaPlayer.start();
if (getIntent() != null)
{
double lat = getIntent().getDoubleExtra("lat",-1.0);
double lng = getIntent().getDoubleExtra("lng",-1.0);
getDirection(lat,lng);
}
}
private void getDirection(double lat,double lng) {
String requestApi = null;
try
{
requestApi = "https://maps.googleapis.com/maps/api/directions/json?"+
"mode=driving&"+
"transit_routing_preference=less_driving&"+
"origin="+Common.mlastlocation.getLatitude()+","+Common.mlastlocation.getLongitude()+"&"+
"destination="+lat+","+lng+"&"+"key="+
getResources().getString(R.string.google_direction_api);
Log.d("D-ride",requestApi);
mService.getPath(requestApi)
.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
JSONObject jsonObject = new JSONObject(response.body().toString());
JSONArray routes = jsonObject.getJSONArray("routes");
JSONObject object = routes.getJSONObject(0);
JSONArray legs = object.getJSONArray("legs");
JSONObject legsobject = legs.getJSONObject(0);
JSONObject distance = legsobject.getJSONObject("distance");
txtDistance.setText(distance.getString("text"));
JSONObject time = legsobject.getJSONObject("duration");
txtTime.setText(distance.getString("text"));
String address = legsobject.getString("end_address");
txtAddress.setText(address);
}catch (JSONException e)
{
e.printStackTrace();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Toast.makeText(CustomerCall.this,""+t.getMessage(),Toast.LENGTH_SHORT).show();
}
});