调用setValue时应用程序崩溃

时间:2017-04-05 19:33:52

标签: android firebase firebase-realtime-database

在主要活动中,工作正常,Firebase没有问题。在所有其他活动中,Firebase会使用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 个答案:

答案 0 :(得分:-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)
        }