当我使用以下代码...并按下应用程序中的删除按钮时,该值将在firebase数据库中删除,但会导致我的应用程序崩溃。
mSingleRemoveBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
if(mPostKey!=null)
{
System.out.println("The key is " + mPostKey);
mDatabase.child(mPostKey).removeValue();
Intent mainIntent = new Intent(BlogSingleActivity.this, MainActivity.class);
startActivity(mainIntent);
}
else
{
System.out.println("The key is empty");
}
}
当我删除" mDatabase.child(mPostKey).removeValue();" 时,该应用程序运行正常,但显然没有从我的数据库中删除该条目。
logcat的
08-01 19:42:51.093 32536-32536/com.vks_apps.blogs I/System.out: The key is -KqT82kPqOE_iq0UKHnm
--------- beginning of system
08-01 19:42:51.129 32536-4494/com.vks_apps.blogs V/FA: Inactivity, disconnecting from the service
08-01 19:42:51.131 32536-32536/com.vks_apps.blogs D/AndroidRuntime: Shutting down VM
08-01 19:42:51.132 32536-32536/com.vks_apps.blogs E/UncaughtException: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at com.google.firebase.database.DatabaseReference.child(Unknown Source)
at com.vks_apps.blogs.BlogSingleActivity$1.onDataChange(BlogSingleActivity.java:88)
at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
at com.google.android.gms.internal.zzboc$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:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
08-01 19:42:51.135 32536-4494/com.vks_apps.blogs D/FA: Logging event (FE): _ae, Bundle[{_o=crash, _sc=BlogSingleActivity, _si=-4635728123048285128, timestamp=1501596771132, fatal=1}]
08-01 19:42:51.157 32536-4494/com.vks_apps.blogs V/FA: Using measurement service
08-01 19:42:51.157 32536-4494/com.vks_apps.blogs V/FA: Connecting to remote service
--------- beginning of crash
08-01 19:42:51.378 32536-32536/com.vks_apps.blogs E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vks_apps.blogs, PID: 32536
java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at com.google.firebase.database.DatabaseReference.child(Unknown Source)
at com.vks_apps.blogs.BlogSingleActivity$1.onDataChange(BlogSingleActivity.java:88)
at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
at com.google.android.gms.internal.zzboc$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:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
08-01 19:42:51.445 32536-5218/com.vks_apps.blogs I/FirebaseCrash: Sending crashes
08-01 19:42:52.021 32536-5218/com.vks_apps.blogs I/FirebaseCrash: Response code: 200
08-01 19:42:52.023 32536-5218/com.vks_apps.blogs I/FirebaseCrash: Report sent with crash report id: 6e22679504000000
答案 0 :(得分:1)
当您使用logcat sais Can't pass null for argument 'pathString' in child()
时,这意味着此代码行中的某些内容为null
:
mDatabase.child(mPostKey).removeValue();
唯一可以为null的是mPostKey
。在使用nullity
方法之前检查removeValue()
。
if(mPostKey != null) {
mDatabase.child(mPostKey).removeValue();
}
要从Firebase数据库中删除记录,请确保mPostKey不为空。看看哪里变成了空。
查看logcat mPostKey
中显示-KqT82kPqOE_iq0UKHnm
的更新问题,BlogSingleActivity
中第88行发生的错误是问题所在。问题实际上是在第88行,再次使用mPostKey
导致应用程序崩溃,因为将空值传递给child()
方法。添加mPostKey != null
仅用于检查无效。
希望它有所帮助。
答案 1 :(得分:0)
在callstack中,您可以看到问题出在BlogSingleActivity$1.onDataChange()
中。在那里,您正在调用数据库并将空值传递给child()
。