简体   繁体   English

当我尝试重新登录时,Android应用程序崩溃

[英]Android app crashes when i try to reLogin

I have an android app that is connected to an API through retrofit, ive succesfully logged in, if i press back button to return back to the login activity again, if i try re-logging in again, the app crashes and give me a NullPointerException. 我有一个通过改版连接到API的android应用,我已经成功登录,如果我按“后退”按钮再次返回到登录活动,如果我尝试重新登录,该应用将崩溃并给我一个NullPointerException 。

here's connection code 这是连接代码

    private void loginUser(String email, String password) {

    UnifyAuthenticationApiInterface service = this.client.create(UnifyAuthenticationApiInterface.class);
    Call<UnifyAuthenticationApiResponse> call = service.staffLogin(email, password);

    call.enqueue(new Callback<UnifyAuthenticationApiResponse>() {
        @Override
        public void onResponse(Call<UnifyAuthenticationApiResponse> call,
                               Response<UnifyAuthenticationApiResponse> response) {

            UnifyAuthenticationApiResponse result = response.body();
            School school = new School();
            com.peterstev.unify.login.Data data = result.getData();
            mySchoolsList = new ArrayList<School>();
            mySchoolsList = data.getSchools();
            staff = data.getStaff();

            gotoHomeActivity();

        }

        @Override
        public void onFailure(Call<UnifyAuthenticationApiResponse> call, Throwable t) {
            progressDialog.dismiss();
            Toast.makeText(MainActivity.this, "Login Failed @ onFailure", Toast.LENGTH_SHORT).show();
        }
    });
}

and the goToHomeActivity() is 而goToHomeActivity()是

 private void gotoHomeActivity() {
    progressDialog.dismiss();
    if (mySchoolsList.size() > 1) {
        schoolsListView = new ListView(MainActivity.this);
        schoolsArrayAdapter = new SchoolListAdapter(MainActivity.this, android.R.layout.simple_list_item_1, mySchoolsList);
        schoolsListView.setAdapter(schoolsArrayAdapter);

        dialog = new Dialog(MainActivity.this);
        dialog.setContentView(schoolsListView);
        dialog.setTitle("Welcome " + staff.getFullName());
        dialog.show();

    } else {
        Intent intent = new Intent(MainActivity.this, NavMainActivity.class);
        startActivity(intent);
    }
}

the NullPointerException gets thrown at NullPointerException被抛出

com.peterstev.unify.login.Data data = result.getData();

at first, it gets the data n succesfully logs in, but when i use the back button n try to log in again it crashes. 起初,它获取的数据n已成功登录,但是当我使用后退按钮n尝试再次登录时,它崩溃了。

Debugger is your answer - check if you aren't loosing any data when going back - maybe you're storing login params somewhere in activity class but you're not saving instance state properly and second request is triggered without necessary data. 调试器是您的答案-检查返回时是否没有丢失任何数据-也许您将登录参数存储在活动类中的某个位置,但您没有正确保存实例状态,并且第二次请求在没有必要数据的情况下被触发。 Check state of variables just before calling your request first and second time. 在第一次和第二次调用您的请求之前,请检查变量的状态。

In situation like that always best bet to place breakpoint and trigger your work step by step. 在这种情况下,最好选择放置断点并逐步触发您的工作。 You cannot be good developer without debugger skills. 没有调试技巧,您就不能成为优秀的开发人员。

I think for some reason, the data object wasn't receiving the result when i used the back button to navigate to the parent activity. 我认为由于某些原因,当我使用后退按钮导航到父活动时,数据对象没有收到结果。 so i used and if condition to make it get the required data. 所以我用,如果条件,使其获得所需的数据。

 private void loginUser(String email, String password) {

    UnifyAuthenticationApiInterface service = this.client.create(UnifyAuthenticationApiInterface.class);
    Call<UnifyAuthenticationApiResponse> call = service.staffLogin(email, password);

    call.enqueue(new Callback<UnifyAuthenticationApiResponse>() {
        @Override
        public void onResponse(Call<UnifyAuthenticationApiResponse> call,
                               Response<UnifyAuthenticationApiResponse> response) {

            if(response.isSuccessful()) {

                UnifyAuthenticationApiResponse result = response.body();
                School school = new School();
                data = result.getData();
                if(data == null) {
                    try{
                        this.onResponse(call, response);
                    }catch(NullPointerException NPE){
                        Log.d("NPE", NPE.getMessage());
                    }
                }
                mySchoolsList = new ArrayList<School>();
                mySchoolsList = data.getSchools();
                staff = data.getStaff();

                gotoHomeActivity();
            }
        }

        @Override
        public void onFailure(Call<UnifyAuthenticationApiResponse> call, Throwable t) {
            progressDialog.dismiss();
            Toast.makeText(MainActivity.this, "Login Failed @ onFailure", Toast.LENGTH_SHORT).show();
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM