2017-04-05 200 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) 
     }