2017-03-15 145 views
-2

我試圖將數據保存到Firebase數據庫。真的沒有那麼複雜,但我發現我的應用程序崩潰,每當我點擊saveInfoButton。我對Swift/iOS的Firebase非常有經驗,但對Android更新。似乎這不會成爲Firebase的問題。Android:App嘗試將數據保存到Firebase數據庫時崩潰

特定消息(當我點擊按鈕時)是:「Restaurant_app已停止,再次打開應用程序。」

activity_main_page.xml

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main_page" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="16dp" 
    android:paddingLeft="64dp" 
    android:paddingRight="64dp" 
    android:paddingTop="16dp" 
    tools:context="com.example.test.restaurant_app.MainPage"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true"> 

     <TextView 
      android:text="Email" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:id="@+id/userEmail" /> 

     <EditText 
      android:layout_margin="15dp" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="textEmailAddress" 
      android:hint="Enter your name" 
      android:id="@+id/nameTextField" 
      /> 

     <EditText 
      android:layout_margin="15dp" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="text" 
      android:hint="address" 
      android:id="@+id/addressTextField" 
      /> 
     <EditText 
      android:layout_margin="15dp" 
      android:id="@+id/ageTextField" 
      android:hint="Age" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
     <Button 
      android:layout_margin="15dp" 
      android:id="@+id/saveInfoButton" 
      android:text="Save Information" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
     <Button 
      android:text="Logout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:id="@+id/logoutButton" /> 
    </LinearLayout> 
</RelativeLayout> 

UserDetails.java

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; 
    } 
} 

MainPage.java

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! 
+0

請張貼完整的堆棧跟蹤! – Chisko

+0

[爲什麼null是一個對象以及null和undefined之間的區別是什麼?](http://stackoverflow.com/questions/801032/why-is-null-an-object-and-whats-the-difference -between-null-unde-undefined) – Chisko

回答

1

看起來像dbReference從未初始化。這將導致行dbReference.child("Users").child(user.getUid()).setValue(userInfo);上的NullPointerException。確保在訪問對象之前初始化對象。

+0

感謝您的回答......當我添加「FirebaseUser user = mAuth.getCurrentUser();」時,問題似乎已解決。到我的saveInfo()函數。在這之前,我在onCreated()函數中使用了這個函數,但由於某種原因,它仍然創建了一個錯誤,直到我將它專門放在函數本身中。 – user2411290

1

下面的代碼添加到saveInfo方法的頂部或初始化這些變量

FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference dbReference= database.getReference();

相關問題