服务(Android通知):尝试在空对象引用上调用虚方法'boolean java.lang.String.equals(java.lang.Object)'

时间:2017-12-06 08:00:03

标签: java android

自从过去3个月以来,我是Android Studio新手。关于我的错误的任何想法如下?谢谢!一旦车手申请的状态等于0,将收到通知。

下面是我的错误,Service java类和模型java类。我已经在MainActivity.java中调用了服务,但是一旦进入MainActvity,应用程序崩溃,但下面有错误。

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.mariamsyafiqah.coolblogserver, PID: 6206
              java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                  at com.example.mariamsyafiqah.coolblogserver.Service.ListenRiderApplication.onChildAdded(ListenRiderApplication.java:56)
                  at com.google.android.gms.internal.zzbox.zza(Unknown Source)
                  at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
                  at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

ListenRiderApplication.java

public class ListenRiderApplication extends Service implements ChildEventListener {
FirebaseDatabase db;
DatabaseReference riderApplication;

@Override
public void onCreate() {
    super.onCreate();
    db = FirebaseDatabase.getInstance();
    riderApplication = db.getReference("Rider");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    riderApplication.addChildEventListener(this);
    return super.onStartCommand(intent, flags, startId);
}

public ListenRiderApplication() {
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    //Trigger here
    RiderApplication request = dataSnapshot.getValue(RiderApplication.class);
    if(request.getStatus().equals("0"))
        showNotification(dataSnapshot.getKey(), request);
}

private void showNotification(String key, RiderApplication request) {
    Intent intent = new Intent(getBaseContext(), RiderAppStatus.class);
    //intent.putExtra("userPhone", request.getPhone());
    PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(),0,intent,0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext());

    builder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setTicker("Rider Coolblog")
            .setContentInfo("New rider registration")
            .setContentText("You have new rider registered #"+key)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(contentIntent);

    NotificationManager manager = (NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);

    //If you want to many notification show, you need to give uniqie ID for each Notification
    int randomInt = new Random().nextInt(9999-1)+1;
    manager.notify(randomInt,builder.build());

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
}

RiderApplication.java

public class RiderApplication {
private String phone, name, availability, noPlate;
private String status;

public RiderApplication() {}

public RiderApplication(String phone, String availability, String name, String noPlate) {
    this.phone = phone;
    this.availability = availability;
    this.name = name;
    this.noPlate = noPlate;
    this.status = "0";
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }

public String getAvailability() {
    return availability;
}

public void setAvailability(String availability) {
    this.availability = availability;
}

public String getNoPlate() {
    return noPlate;
}

public void setNoPlate(String noPlate) {
    this.noPlate = noPlate;
}
}

1 个答案:

答案 0 :(得分:-2)

在onChildAdded方法中尝试以下代码

if(request.getStatus()!=null && request.getStatus().equals("0"))
        showNotification(dataSnapshot.getKey(), request);