0

我學習Android和跨越很奇怪的行爲來。我在Android中使用Retrofit2作爲REST庫(使用異步調用)。我想發送authCode和TokenId作爲谷歌在我的服務器上的建議。當我檢查用戶是否設置了密碼時,我會做出迴應。如果我返回代碼206意味着用戶尚未在我的後端服務器上設置密碼。 所以我想開始一個片段,用戶將輸入密碼(我也說,我定義了LoginFragment和RegistrationFragment這兩個工作在這個活動)。但是這裏有一個技巧,Fragment被調用,當我的onCreateView被執行時,TextView和Button的值爲null,爲什麼?我認爲有一個問題,因爲這是在後臺線程運行,但我可能會誤解。有沒有更好的方法來實現我想要的?調用片段收到改裝響應(TextView的和按鈕都在裏面片段空)

  • 我改造的呼叫:

    private void sendTokenToServer(String idToken, String authCode, String email){ 
        Log.d("APP", authCode); 
        GoogleTokenRequest googleTokenRequest = new GoogleTokenRequest(); 
        googleTokenRequest.setTokenId(idToken); 
        googleTokenRequest.setAuthCode(authCode); 
        googleTokenRequest.setEmail(email); 
        ApiInterface apiService = 
         ApiClient.getClient().create(ApiInterface.class); 
    
    
    
        Call<GoogleTokenRequest> call = apiService.sendTokenToBackend(googleTokenRequest); 
        Log.d("APP", call.toString()); 
    
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    
    
        call.enqueue(new Callback<GoogleTokenRequest>() { 
        //execute za sinhroni klic, enqueue za asinhroni 
        @Override 
        public void onResponse(Call<GoogleTokenRequest> call, Response<GoogleTokenRequest> response) { 
          String access_token = response.headers().get("Authorization"); 
          Log.d("APP", access_token); 
          prefs.edit().putString("access_token",access_token).commit(); 
          int statusCode = response.code(); 
    
          if (statusCode == 206) { 
           SetPasswordFragment registerFragment = new SetPasswordFragment(); 
           FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
           fragmentTransaction.replace(R.id.fragment_login_container, registerFragment); 
           fragmentTransaction.commit(); 
          } 
          else{ 
           Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
           startActivity(intent); 
    
           } 
    
  • 這是我的片斷代碼:

    公共類SetPasswordFragment擴展片段{

    private OnSetPasswordListener onSetPasswordListener; 
    public SetPasswordFragment() { 
        // Required empty public constructor 
    } 
    
    @Override 
    public void onAttach(Context context) { 
        super.onAttach(context); 
        if (context instanceof OnSetPasswordListener) { 
         onSetPasswordListener = (OnSetPasswordListener) context; 
        } else { 
         throw new RuntimeException(context.toString() 
           + " must implement OnFragmentInteractionListener"); 
        } 
    } 
    
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
             Bundle savedInstanceState) { 
        // Inflate the layout for this fragment 
        super.onCreate(savedInstanceState); 
        LayoutInflater lf = getActivity().getLayoutInflater(); 
    
        View rootView = lf.inflate(R.layout.fragment_set_password, container, false); 
        Button setPasswordButton = (Button) rootView.findViewById(R.id.btn_set_password_ok); 
        TextView textView = (TextView) rootView.findViewById(R.id.set_password_message); 
    
        Log.d("APP",rootView.toString()); 
    
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
        String username = prefs.getString("username", "User"); 
    
        textView.setText(String.format(getString(R.string.set_password_message), username)); 
    
        setPasswordButton.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          setPasswordValidate(); 
         } 
        }); 
    
        return rootView; 
    } 
    
  • 這是logcat的: enter image description here

+1

你只能膨脹並返回onCreateView中的視圖,並在片段的onViewCreated方法中執行所有其他Button和TextView的東西 –

+0

從來沒有想過它,會嘗試,讓我們希望它的作品。 – alphiii

+0

的可能的複製[什麼是空指針異常,怎麼解決呢?(http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) –

回答

0

我知道我的問題描述並不指向我發現但我仍然會發布它,因爲也許這將幫助別人真正的問題。我的情況下,它是XML文件中的問題我剛剛刪除android:fillViewport =「true」或將其設置爲false,它的工作原理。

1

這也許應該修復它:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    super.onCreate(savedInstanceState); 

    View rootView = inflater.inflate(R.layout.fragment_set_password, container, false); 
    return rootView; 
} 

@Override 
public void onViewCreated(View view){ 
Button setPasswordButton = (Button) view.findViewById(R.id.btn_set_password_ok); 
    TextView textView = (TextView) view.findViewById(R.id.set_password_message); 


    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
    String username = prefs.getString("username", "User"); 

    textView.setText(String.format(getString(R.string.set_password_message), username)); 

    setPasswordButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      setPasswordValidate(); 
     } 
    }); 

} 

注:我不rexactly記得確切的參數爲onViewCreated方法,但鑑於肯定是存在的。所以我想這應該工作。

+0

可惜這不起作用,它和onCreateView中的一樣。如果我刪除setText和setOnclickListener它的作品。但那不是我想要的。參考(R.id ....)設置正確。 – alphiii

+0

你可以分享實際發生的logcat嗎? –

+0

它說textView.setText(String.format(getString(R.string.set_password_message),用戶名))上的空對象引用; – alphiii