2017-09-03 28 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();