2017-09-03 6 views
0

私が初めてFirebaseで働いて、私の既存のAndroidを変更しようとしていますが、それが提供していますリアルタイムデータベースを使用するようにアプリケーション(これに私はまだ比較的新しいです)。私のアプリは、アプリのライフサイクルの間に、ユーザーの名前を保存するために開いたときと、ゲームのスコア/日付を保存すると判断したときの2つの別々のポイントでデータを送信します。私のオプションを読み取り、Firebase DBにSQLから変更する方法を研究して、それが作成するつもりだった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); 
    } 
} 

私はgetters/setterを含む別個のUserクラスを持っており、ApplicationのマニフェストにUserクラスを定義しました。私の問題は、私のログは、キーが正常に生成され、gameID変数に渡されたことを示していますが、アプリケーションを保存しようとするとクラッシュします。私の最初の質問は、彼らはそれの外で私がしようとしている方法を使用することができているので、私は見つけることができるグローバル変数のすべての例では、それらを単独のonCreateメソッドで使用されている示して?もしそうなら、誰でも私が間違ったことを見ることができます。 「globalVar.setID(gameID);」それは

が現状だとき、私はデバッグ中にNullPointerExceptionを受け付けております私はなぜgameID変数が正常に作成されているのか理解できません。どんな助けもありがとう、私は本当に私の知恵の終わりです。

答えて

1

まず、私はより説明的なものにUserをリネーム検討します。 Applicationを拡張しているので、通常はAppという名前にします。このクラスは単なるUserではないので、それはグローバル変数やユーザ情報を含むことができ、あなたの全体のAndroidアプリ、です。

第2に、onCreateでは、その範囲に新しいglobalVar変数を作成しているため、NullPointerExceptionが発生している理由があります。

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

変更

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

関連する問題