-4
私は過去3ヶ月からAndroidスタジオを初めて使用しています。以下のエラーのアイデアは?ありがとう!ライダーアプリケーションのステータスが0になると通知が届きます。サービス(Android Notification):ヌルオブジェクト参照で仮想メソッド 'boolean java.lang.String.equals(java.lang.Object)'を呼び出そうとしています
以下は私のエラー、サービス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;
}
}
おかげで、次のコードを試してみてください!私のエラーを解決:) –