2017-09-03 64 views
0

我在Firebase中第一次工作,並試圖修改我現有的Android應用程序以使用它提供的實時數據庫。我的應用程序在應用程序生命週期的兩個獨立點上發送數據:打開該應用程序以存儲用戶的姓名以及存儲遊戲得分/日期的結果。在閱讀我的選項和研究如何從SQL更改爲Firebase DB時,似乎使用全局變量來存儲將要創建的gameID密鑰是一個很好的選擇,但似乎無法使其運行。應用程序結構如下。使用全局變量來存儲Android Firebase getKey?

public class EditTextButtons extends AppCompatActivity implements View.OnClickListener { 

    EditText etName; 
    Button btnAdd; 
    Button btnLastFive; 
    Button btnTopFive; 

    User globalVar; 

    FirebaseDatabase database = FirebaseDatabase.getInstance(); 
    DatabaseReference mDatabase = database.getReference(); 
    DatabaseReference gamesDB = mDatabase.child("Games"); 

    /** 
    * Called when Activity is first created. Instantiates the database, sets up 
    * the content views, and creates the text field and buttons. Also holds commands 
    * to insert data into database for username. 
    * 
    */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate (savedInstanceState); 
     setContentView (R.layout.snake_layout_r); 

     etName = (EditText) findViewById (R.id.etName); 
     btnAdd = (Button) findViewById (R.id.btnAdd); 
     btnLastFive = (Button) findViewById (R.id.btnLastFive); 
     btnTopFive = (Button) findViewById (R.id.btnTopFive); 

     // set listeners 
     btnAdd.setOnClickListener (this); 
     btnLastFive.setOnClickListener (this); 
     btnLastFive.setOnClickListener (this); 

     //Call Application class User 
     final User globalVar = (User) getApplicationContext(); 
    } 

    /** 
    * Sets up the method to handle button clicks 
    * btnAdd calls the method to insert username to database then switch to the game start screen 
    * btnLastFive calls the message to display the scores of the last 5 played games 
    */ 
    @Override 
    public void onClick(View v) { 
     switch (v.getId ()) { 
      case R.id.btnAdd: 
       insertIntoDB(); 
       Intent i = new Intent (getApplicationContext (), Snake.class); 
       startActivity (i); 
       break; 
      case R.id.btnLastFive: 
       lastFive(); 
       break; 
      case R.id.btnTopFive: 
       lastFive(); 
       break; 
      default: 
       break; 
     } 
    } 

    /** 
    * Sets up the method to insert username into database 
    */ 
    protected void insertIntoDB() { 
     String name = etName.getText().toString(); 

     //Username is required 
     if(TextUtils.isEmpty (name)) { 
      etName.setError("Required"); 
      return; 
     } 

     String gameID = gamesDB.push().getKey(); 
     Log.d ("key", gameID); 
     globalVar.setID(gameID); 


     Map<String, User> users = new HashMap<String, User>(); 
     users.put(gameID, new User(name)); 
     gamesDB.setValue(users); 
    } 
} 

我有一個單獨的用戶類,其中包含所述的getter/setter和我在清單下申請中所定義的用戶類別。我的問題是,即使我的日誌顯示密鑰正在成功生成並傳遞給gameID變量,當我嘗試存儲它時,應用程序崩潰。我能找到的每個全局變量示例都顯示它們僅用於onCreate方法,所以我的第一個問題是它們可以在我嘗試做的方式之外使用嗎?如果是這樣,任何人都可以看到我做錯了什麼。

在它的當前狀態下,我在調試時收到一個nullPointerException,當「globalVar.setID(gameID);」行被調用,但我不明白爲什麼自gameID變量被成功創建。任何幫助表示讚賞,我真的在我的智慧結束。

回答

1

首先,我會考慮將User重命名爲更具描述性的內容。既然你延伸Application,我通常只是將其命名爲App。因爲這個類不只是User,它是你的整個Android應用程序,可能包含全局變量或用戶信息。

其次,在您的onCreate中,您遇到NullPointerException的原因是因爲您在該範圍內創建了一個新的globalVar變量。

變化

//Call Application class User 
final User globalVar = (User) getApplicationContext(); 

//Call Application class User 
globalVar = (User) getApplicationContext();