2017-04-05 197 views
-1

在主要活動中,工作正在進行,Firebase沒有問題。在所有其他活動中,Firebase使用setValue發送數據,但之後應用程序崩潰。爲什麼?調用setValue時應用程序崩潰

public class main_activity extends AppCompatActivity { 


    static String FirstOption; 
    static String OptionFour; 
    static String SecondOption; 
    static String ThirdOption; 
    static String TheQuestion; 
    static String CorrectAnswer; 
    public static String[] Str = new String[100]; 
    private int index_yoni=0; 
    private TextView MainPageTitle; 
    private FirebaseAuth firebaseAuth; 
    private Firebase fire; 
    private String Uid; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_activity); 



     Firebase.setAndroidContext(main_activity.this); 
     fire = new Firebase("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/"); 



     MainPageTitle=(TextView) findViewById(R.id.textView3); 

      fire.addValueEventListener(new ValueEventListener() { 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       firebaseAuth=FirebaseAuth.getInstance(); 
       FirebaseUser user=firebaseAuth.getCurrentUser(); 



      if (firebaseAuth.getCurrentUser() != null) { 
       MainPageTitle.setText(dataSnapshot.child("Userinformation").child(user.getUid()).child("name").getValue()+" "+"'welcome"); 
      } 
      else 
      { 
       MainPageTitle.setText("'welcom to the game"); 
      } 

      } 
    public void onCancelled(FirebaseError firebaseError) { 
     Log.e("the error ....", firebaseError.getMessage()); 
    } 
      }); 


     fire.addValueEventListener(new ValueEventListener() { 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       for (DataSnapshot SportSnapshot : dataSnapshot.child(TypesOfTests.Topic).getChildren()) { 
        SportsQuestions sportsQuestions = SportSnapshot.getValue(SportsQuestions.class); 
        FirstOption = sportsQuestions.getFirstOption(); 
        OptionFour = sportsQuestions.getOptionFour(); 
        SecondOption = sportsQuestions.getSecondOption(); 
        ThirdOption = sportsQuestions.getThirdOption(); 
        CorrectAnswer = sportsQuestions.getCorrectAnswer(); 
        TheQuestion = sportsQuestions.getTheQuestion(); 
        increment(); 
       } 
      } 
      public void onCancelled(FirebaseError firebaseError) { 
       Log.e("the error ....", firebaseError.getMessage()); 
      } 
     } 




    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_profile2); 

     Firebase.setAndroidContext(ProfileActivity.this); 
     fire= new Firebase("https://triviaproject-4c91e.firebaseio.com/"); 

     fire.child("Userinformation").child(user.getUid()).setValue(userinformation); 
+1

如果遇到碰撞時,總是包括墜毀的記錄,否則我們很難弄清楚發生了什麼事情。 –

回答

-1
fire.addValueEventListener(new ValueEventListener() { 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      for (DataSnapshot SportSnapshot : dataSnapshot.child(TypesOfTests.Topic).getChildren()) { 
       SportsQuestions sportsQuestions = SportSnapshot.getValue(SportsQuestions.class); 
       FirstOption = sportsQuestions.getFirstOption(); 
       OptionFour = sportsQuestions.getOptionFour(); 
       SecondOption = sportsQuestions.getSecondOption(); 
       ThirdOption = sportsQuestions.getThirdOption(); 
       CorrectAnswer = sportsQuestions.getCorrectAnswer(); 
       TheQuestion = sportsQuestions.getTheQuestion(); 
       increment(); 
      } 
      //you need to add this add this to stop even 
      fire.removeEventListener(this) 
     }