我有一個通過改進連接到API的android應用,如果我按回退按鈕返回到登錄活動,如果我嘗試重新登錄,則成功登錄在再次,該應用程序崩潰,並給我一個NullPointerException。當我嘗試重新登錄時,Android應用崩潰
這裏的連接代碼
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();
}
});
}
和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);
}
}
的NullPointerException異常被在
com.peterstev.unify.login.Data data = result.getData();
首先拋出,它得到的數據n成功地記錄在,但是當我使用後退按鈕ñ嘗試再次登錄它崩潰。
無法判斷您的後端調用返回的內容。放置導致NPE並在調試模式下運行的斷點。然後只是檢查爲什麼結果爲空。也許某種保護措施太頻繁地調用登錄。嘗試用Postman或類似工具重新創建這種情況。 – 3mpty
您是否清除點擊後退按鈕的所有數據? @Light –
我並未清除任何數據。或者當我回去時必須重新創建活動? @Partha – Light