我是一名Android Studio编码初学者,目前正在开发一个营养应用程序以进行编程练习。我使用Firebase进行身份验证,并将其用作保存用户数据的数据库。

怎么运行的:

我的应用程序内置了一项调查,其中要求询问身体的具体特征和口味(年龄,身高,喜欢/不喜欢的成分等)。我有一个具有公共静态属性的GlobalUser类,用于将答案保存在应用程序中。用户注册后,他将被直接发送到调查活动。在那里,他回答了问题,并将结果以其UID写入Firebase数据库(我使用与GlobalUser具有相同属性的User类来创建实例,并使用Firebase的setValue(Object)方法)。如果他登录(或仍处于登录状态),则LoginRegistrationActivity将其直接发送给MainActivity。在那里,GlobalUser类使用其UID下保存的数据实例化。他可以从MainActivity导航到ProfileActivity,在UI上根据他的数据更新UI。这很好。完成调查后,我可以在由用户的UID组成的子节点中找到结果,UI得到正确更新,并且登录/注册过程按预期进行。

怎么了:

但是,当我玩弄不同的设计并不断重新启动该应用程序时,它偶尔会崩溃。经过一些测试,结果表明GlobalUser类未更新,因此ArrayLists为空,当我在它们上使用.size()时,导致NullPointerExceptions。由于此问题很少发生,并且似乎与多次重新启动应用程序有关,因此我认为这与Activity生命周期有关,因此我也在onStart和onResume中更新了GlobalUser,但没有帮助。我还尝试在设置ArrayLists之前直接在ProfileActivity中再次更新GlobalUser,但是它不起作用。我仍然猜想它与生命周期有关,但是我不知道应该从哪里开始。以下是相关类/活动的代码:

LoginRegistration活动:

public class LoginRegistrationActivity extends AppCompatActivity {
    private DatabaseReference mRef;
    private FirebaseAuth mAuth;
    private EditText emailAddress;
    private EditText emailPassword;
    private Button emailLogin;
    private Button emailRegistration;
    private TextView forgotPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_registration);
        mAuth = FirebaseAuth.getInstance();

        if (mAuth.getCurrentUser()!=null){
            Intent i = new Intent (LoginRegistrationActivity.this, MainActivity.class);
            LoginRegistrationActivity.this.startActivity(i);
        }

        emailAddress = findViewById(R.id.address_edit);
        emailPassword = findViewById(R.id.password_edit);
        emailLogin = findViewById(R.id.mail_login_button);
        emailRegistration = findViewById(R.id.mail_registration_button);
        forgotPassword = findViewById(R.id.forgot_password);

        emailRegistration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email = emailAddress.getText().toString().trim();
                String password = emailPassword.getText().toString().trim();

                if (TextUtils.isEmpty(email)){
                    Toast.makeText(LoginRegistrationActivity.this, "Bitte E-Mail Addresse eingeben!", Toast.LENGTH_LONG).show();
                    return;
                }

                if (TextUtils.isEmpty(password)){
                    Toast.makeText(LoginRegistrationActivity.this, "Bitte Passwort eingeben!", Toast.LENGTH_LONG).show();
                    return;
                }

                if (password.length()<6){
                    Toast.makeText(LoginRegistrationActivity.this, "Passwort muss mindestens sechs Zeichen lang sein!", Toast.LENGTH_LONG).show();
                    return;
                }

                mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(LoginRegistrationActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (!task.isSuccessful()){
                            Toast.makeText(LoginRegistrationActivity.this, "Unbekannter Fehler", Toast.LENGTH_LONG).show();
                        } else {
                            Intent i = new Intent (LoginRegistrationActivity.this, SurveyGreetingActivity.class);
                            LoginRegistrationActivity.this.startActivity(i);
                            finish();
                        }
                    }
                });
            }
        });

        emailLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String password = emailPassword.getText().toString();
                String email = emailAddress.getText().toString();

                if (TextUtils.isEmpty(email)){
                    Toast.makeText(LoginRegistrationActivity.this, "Bitte E-Mail Addresse eingeben!", Toast.LENGTH_LONG).show();
                    return;
                }

                if (TextUtils.isEmpty(password)){
                    Toast.makeText(LoginRegistrationActivity.this, "Bitte Passwort eingeben!", Toast.LENGTH_LONG).show();
                    return;
                }

                if (password.length()<6){
                    Toast.makeText(LoginRegistrationActivity.this, "Passwort muss mindestens sechs Zeichen haben!", Toast.LENGTH_LONG).show();
                    return;
                }

                mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(LoginRegistrationActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (!task.isSuccessful()){
                            Toast.makeText(LoginRegistrationActivity.this, "Unbekannter Fehler beim Einloggen", Toast.LENGTH_LONG).show();
                        } else {
                            Intent i = new Intent (LoginRegistrationActivity.this, MainActivity.class);
                            LoginRegistrationActivity.this.startActivity(i);
                            finish();
                        }
                    }
                });
            }
        });
    }
}


主要活动:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final FirebaseAuth mAuth = FirebaseAuth.getInstance();
        FirebaseUser user = mAuth.getCurrentUser();
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference mRef = database.getReference().child("users").child("uid").child(mAuth.getCurrentUser().getUid());


        //In case the user cancelled the app when filling out the survey for the first time
        if (mRef == null){
            MainActivity.this.startActivity(new Intent (MainActivity.this, SurveyGreetingActivity.class));
        }

        //sets GlobalUser to data saved in Firebase Database User object
        mRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                User user = dataSnapshot.getValue(User.class);
                if (user!=null){
                    GlobalUser.setToUser(user);
                    GlobalUser.setGlobalUid(mAuth.getCurrentUser().getUid());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(getApplicationContext(), "Database Error", Toast.LENGTH_LONG).show();
            }
        });
    }
}


GlobalUser:

package com.example.andre.valetto02;
import java.util.ArrayList;
public class GlobalUser {

    public static String globalUid = null;

    public static ArrayList<Ingredient> globalLikes;

    public static ArrayList<Ingredient> globalDislikes;

    public static int globalAge;

    public static int globalWeight;

    public static int globalHeight;

    public static int globalTrainingGoal;

    public static int globalDailyActive;

    public static boolean globalIsMale;

    public GlobalUser() {
    }

    public static String getGlobalUid() {
        return globalUid;
    }

    public static void setGlobalUid(String globalUid) {
        GlobalUser.globalUid = globalUid;
    }

    public static ArrayList<Ingredient> getGlobalLikes() {
       return globalLikes;
    }

    public static void setGlobalLikes(ArrayList<Ingredient> globalLikes) {
        GlobalUser.globalLikes = globalLikes;
    }

    public static ArrayList<Ingredient> getGlobalDislikes() {
        return globalDislikes;
    }

    public static void setGlobalDislikes(ArrayList<Ingredient> globalDislikes) {
        GlobalUser.globalDislikes = globalDislikes;
    }

    public static int getGlobalAge() {
        return globalAge;
    }

    public static void setGlobalAge(int globalAge) {
        GlobalUser.globalAge = globalAge;
    }

    public static int getGlobalWeight() {
        return globalWeight;
    }

    public static void setGlobalWeight(int globalWeight) {
        GlobalUser.globalWeight = globalWeight;
    }

    public static int getGlobalHeight() {
        return globalHeight;
    }

    public static void setGlobalHeight(int globalHeight) {
        GlobalUser.globalHeight = globalHeight;
    }

    public static int getGlobalTrainingGoal() {
        return globalTrainingGoal;
    }

    public static void setGlobalTrainingGoal(int globalTrainingGoal) {
        GlobalUser.globalTrainingGoal = globalTrainingGoal;
    }

    public static int getGlobalDailyActive() {
        return globalDailyActive;
    }

    public static void setGlobalDailyActive(int globalDailyActive) {
        GlobalUser.globalDailyActive = globalDailyActive;
    }

    public static boolean isGlobalIsMale() {
        return globalIsMale;
    }

    public static void setGlobalIsMale(boolean globalIsMale) {
        GlobalUser.globalIsMale = globalIsMale;
    }

    public static void setToUser(User user) {
       GlobalUser.setGlobalAge(user.getAge());
       GlobalUser.setGlobalWeight(user.getWeight());
       GlobalUser.setGlobalHeight(user.getHeight());
       GlobalUser.setGlobalDailyActive(user.getDailyActive());
       GlobalUser.setGlobalTrainingGoal(user.getTrainingGoal());
       GlobalUser.setGlobalIsMale(user.getIsMale());
       GlobalUser.setGlobalLikes(user.getLikes());
       GlobalUser.setGlobalDislikes(user.getDislikes());
   }

   public static void resetLikesAndDislikes(){
       globalLikes = new ArrayList <>();
       globalDislikes = new ArrayList<>();
   }

   public static User globalToUser () {
       return new User (globalLikes, globalDislikes, globalWeight, globalHeight, globalAge, globalTrainingGoal, globalDailyActive, globalIsMale);
    }
}


用户:

package com.example.andre.valetto02;
import java.util.ArrayList;

 public class User {
    ArrayList<Ingredient> likes;
    ArrayList<Ingredient> dislikes;

    Boolean isMale;

    public Boolean getIsMale(){return isMale;}

    public void setIsMale(Boolean b){isMale = b;}

    public void setDislikes(ArrayList<Ingredient> dislikes) {
        this.dislikes = dislikes;
    }


    public User (){
        likes = new ArrayList<>();
        dislikes = new ArrayList<>();
        weight = 0;
        height = 0;
        age = 0;
        trainingGoal = 2;
        dailyActive = 1;
        isMale=true;
    }

    public User (ArrayList<Ingredient> l, ArrayList<Ingredient> d, int w, int h, int a, int tG, int dA, boolean iM) {
        likes = l;
        dislikes = d;
        weight = w;
        height = h;
        age = a;
        trainingGoal = tG;
        dailyActive = dA;
        isMale = iM;
    }

    int age;

    public ArrayList<Ingredient> getDislikes() {
        return dislikes;
    }

    public ArrayList<Ingredient> getLikes() {
        return likes;
    }

    public void setLikes (ArrayList<Ingredient> list){
        likes = list;
    }

    public void setDisikes (ArrayList<Ingredient> list){
        dislikes = list;
    }

    public int getAge () {
        return age;
    }

    public void setAge (int i) {
        age = i;
    }

    int weight;

    public int getWeight (){
        return weight;
    }

    public void setWeight(int i) {
        weight = i;
    }

    int height;

    public int getHeight (){
        return height;
    }

    public void setHeight(int i) {
        height = i;
    }

    int trainingGoal; //trainingGoal = 0 means weight loss, 1 means muscle gain and 2 means healthy living

    public void setTrainingGoal(int i) {
        trainingGoal = i;
    }

    public int getTrainingGoal(){
        return trainingGoal;
    }

    int dailyActive; //dailyActive = 0 means wenig, 1 means leicht, 2 means moderat, 3 means sehr and 4 means extrem

    public int getDailyActive() {return dailyActive;}

    public void setDailyActive(int i) {dailyActive = i;}

    public double computeCalorieGoal(){
        if (isMale) {
            double RMR;
            RMR = weight*10 + 6.25*height - 5*age + 5;
            if (dailyActive==0) {RMR=RMR*1.2;}
            else if (dailyActive==1) {RMR=RMR*1.375;}
            else if (dailyActive==2) {RMR=RMR*1.55;}
            else if (dailyActive==3) {RMR=RMR*1.725;}
            else {RMR=RMR*1.9;}
            if (trainingGoal == 0) {RMR = RMR - 400;}
            else if (trainingGoal ==1){RMR = RMR + 400;}
            return RMR;

        } else {
            double RMR;
            RMR = weight*10 + 6.25*height - 5*age - 161;
            if (dailyActive==0) {RMR=RMR*1.2;}
            else if (dailyActive==1) {RMR=RMR*1.375;}
            else if (dailyActive==2) {RMR=RMR*1.55;}
            else if (dailyActive==3) {RMR=RMR*1.725;}
            else {RMR=RMR*1.9;}
            if (trainingGoal == 0) {RMR = RMR - 300;}
            else if (trainingGoal ==1){RMR = RMR + 300;}
            return RMR;

        }
    }
}


谢谢您的帮助!

最佳答案

我刚刚发现了错误。它与活动生命周期无关,并且仅与重启应用程序间接相关。问题在于Firebase的Value Event Listeners仍然是AsyncTasks。当我启动应用程序并立即打开ProfileActivity时,在Firebase AsyncTask可以从数据库获取数据之前创建了Activity。因此,ProfileActivity将在实例化ArrayList之前对其调用.size()方法。从本质上讲,当您通过UI单击速度过快并且比异步数据获取任务快时,就会发生错误。

因此,我将会话管理移至LoginRegistrationActivity,如下所示:

     if (mAuth.getCurrentUser()!=null){
        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        DatabaseReference mRef = firebaseDatabase.getReference().child("users").child("uid").child(mAuth.getCurrentUser().getUid());
        //In case the user cancelled the app when filling out the survey for the first time
        if (mRef == null){
            LoginRegistrationActivity.this.startActivity(new Intent (LoginRegistrationActivity.this, SurveyGreetingActivity.class));
        }
        mRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                User user = dataSnapshot.getValue(User.class);
                if (user!=null) {
                    GlobalUser.setToUser(user);
                    GlobalUser.setGlobalUid(mAuth.getCurrentUser().getUid());
                }
                Intent i = new Intent (LoginRegistrationActivity.this, MainActivity.class);
                LoginRegistrationActivity.this.startActivity(i);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }


通过将LoginRegistrationActivity.this.startActivity(i)移至onDataChange方法,可以确保在MainActivity启动之前实例化GlobalUser变量。可能还有更优雅的方法可以做到这一点。

10-08 03:41