Android:应用程序在试图将数据保存到Firebase数据库时崩溃
我正在尝试将数据保存到Firebase数据库。其实没什么复杂的,但我发现每当我点击saveInfoButton时,我的应用程序就会崩溃。我对Firebase与Swift/iOS的结合非常有经验,但对Android来说比较新。这似乎不是Firebase的问题 我点击按钮时的具体信息是:餐厅应用程序已停止,再次打开应用程序 activity_main_page.xml MainPage.java 错误:Android:应用程序在试图将数据保存到Firebase数据库时崩溃,android,database,firebase,firebase-realtime-database,Android,Database,Firebase,Firebase Realtime Database,我正在尝试将数据保存到Firebase数据库。其实没什么复杂的,但我发现每当我点击saveInfoButton时,我的应用程序就会崩溃。我对Firebase与Swift/iOS的结合非常有经验,但对Android来说比较新。这似乎不是Firebase的问题 我点击按钮时的具体信息是:餐厅应用程序已停止,再次打开应用程序 activity_main_page.xml MainPage.java 错误: 03-15 11:33:41.516 4595-4595/com.example.test.re
03-15 11:33:41.516 4595-4595/com.example.test.restaurant_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test.restaurant_app, PID: 4595
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference
at com.example.test.restaurant_app.MainPage.saveInfo(MainPage.java:65)
at com.example.test.restaurant_app.MainPage.onClick(MainPage.java:77)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
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)
03-15 11:33:41.521 1605-1828/system_process W/ActivityManager: Force finishing activity com.example.test.restaurant_app/.MainPage
03-15 11:33:41.554 1326-2029/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
03-15 11:33:41.557 1326-2029/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
03-15 11:33:41.558 1326-2029/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
03-15 11:33:41.559 1605-3137/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
03-15 11:33:41.559 1605-3137/system_process D/OpenGLRenderer: Swap behavior 1
03-15 11:33:41.560 1605-3137/system_process W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
03-15 11:33:41.560 1605-3137/system_process D/OpenGLRenderer: Swap behavior 0
03-15 11:33:42.027 1605-1619/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{7bfe46d u0 com.example.test.restaurant_app/.MainPage t644 f}
03-15 11:33:46.545 2380-5115/com.google.android.gms W/PlatformStatsUtil: Could not retrieve Usage & Diagnostics setting. Giving up.
03-15 11:33:51.529 1605-1619/system_process W/ActivityManager: Launch timeout has expired, giving up wake lock!
看起来dbReference从未初始化过。这将导致dbReference.childUsers.childuser.getUid.setValueuserInfo;行出现NullPointerException;。确保在访问对象之前对其进行初始化。将以下代码添加到saveInfo方法顶部或初始化这些变量 FirebaseDatabase=FirebaseDatabase.getInstance; DatabaseReference dbReference=database.getReference;
请张贴完整的stacktrace!可能重复感谢您的回答…当我添加FirebaseUser=mAuth.getCurrentUser时,问题似乎已经解决;到我的saveInfo函数。在此之前,我在我的onCreated函数中有这个,但由于某种原因,它仍然创建了一个错误,直到我把它特别放在函数本身中。
package com.example.test.restaurant_app;
public class UserDetails {
public String name;
public String address;
public int age;
public UserDetails(String name, String address, int age) {
this.name = name;
this.address = address;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.example.test.restaurant_app;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
public class MainPage extends AppCompatActivity implements View.OnClickListener {
private FirebaseAuth mAuth;
private FirebaseUser user;
private TextView textViewEmail;
private Button logoutButton;
private DatabaseReference dbReference;
private EditText editTextName, editTextAddress, editTextAge;
private Button saveInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
mAuth = FirebaseAuth.getInstance();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
logoutButton = (Button) findViewById(R.id.logoutButton);
textViewEmail = (TextView) findViewById(R.id.userEmail);
editTextName = (EditText) findViewById(R.id.nameTextField);
editTextAddress = (EditText) findViewById(R.id.addressTextField);
editTextAge = (EditText) findViewById(R.id.ageTextField);
saveInfo = (Button) findViewById(R.id.saveInfoButton);
if (mAuth.getCurrentUser() == null) {
finish();
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
}
FirebaseUser user = mAuth.getCurrentUser();
String name = user.getEmail().toString().trim();
String welcomeMessage = "Hello, " + name;
textViewEmail.setText(welcomeMessage);
logoutButton.setOnClickListener(this);
saveInfo.setOnClickListener(this);
}
public void saveInfo() {
String name = editTextName.getText().toString();
String address = editTextAddress.getText().toString();
int age = Integer.parseInt(editTextAge.getText().toString());
UserDetails userInfo = new UserDetails(name, address, age);
dbReference.child("Users").child(user.getUid()).setValue(userInfo);
Toast.makeText(this, "Information Saved", Toast.LENGTH_LONG).show();
}
public void onClick(View view) {
if (view == logoutButton) {
finish();
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
}
if (view == saveInfo){
saveInfo();
}
}
}
03-15 11:33:41.516 4595-4595/com.example.test.restaurant_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test.restaurant_app, PID: 4595
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference
at com.example.test.restaurant_app.MainPage.saveInfo(MainPage.java:65)
at com.example.test.restaurant_app.MainPage.onClick(MainPage.java:77)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
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)
03-15 11:33:41.521 1605-1828/system_process W/ActivityManager: Force finishing activity com.example.test.restaurant_app/.MainPage
03-15 11:33:41.554 1326-2029/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
03-15 11:33:41.557 1326-2029/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
03-15 11:33:41.558 1326-2029/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
03-15 11:33:41.559 1605-3137/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
03-15 11:33:41.559 1605-3137/system_process D/OpenGLRenderer: Swap behavior 1
03-15 11:33:41.560 1605-3137/system_process W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
03-15 11:33:41.560 1605-3137/system_process D/OpenGLRenderer: Swap behavior 0
03-15 11:33:42.027 1605-1619/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{7bfe46d u0 com.example.test.restaurant_app/.MainPage t644 f}
03-15 11:33:46.545 2380-5115/com.google.android.gms W/PlatformStatsUtil: Could not retrieve Usage & Diagnostics setting. Giving up.
03-15 11:33:51.529 1605-1619/system_process W/ActivityManager: Launch timeout has expired, giving up wake lock!