2016-04-27 83 views
0

我有一個通過改進連接到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成功地記錄在,但是當我使用後退按鈕ñ嘗試再次登錄它崩潰。

+1

無法判斷您的後端調用返回的內容。放置導致NPE並在調試模式下運行的斷點。然後只是檢查爲什麼結果爲空。也許某種保護措施太頻繁地調用登錄。嘗試用Postman或類似工具重新創建這種情況。 – 3mpty

+0

您是否清除點擊後退按鈕的所有數據? @Light –

+0

我並未清除任何數據。或者當我回去時必須重新創建活動? @Partha – Light

回答

1

調試器是你的答案 - 是否會回來時,你不會丟失任何數據 - 活動課的地方,也許你存儲登錄PARAMS,但你無法正常保存實例狀態和第二請求被觸發沒有必要的數據。在第一次和第二次調用請求之前檢查變量的狀態。

在這種情況下,總是最好的選擇是放置斷點並逐步觸發你的工作。如果沒有調試技能,你不能成爲優秀的開發人員。

0

我認爲出於某種原因,當我使用後退按鈕導航到父活動時,數據對象沒有收到結果。所以我用,如果條件使其獲得所需的數據。

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(); 
     } 
    }); 
} 
相關問題