我學習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; }
你只能膨脹並返回onCreateView中的視圖,並在片段的onViewCreated方法中執行所有其他Button和TextView的東西 –
從來沒有想過它,會嘗試,讓我們希望它的作品。 – alphiii
的可能的複製[什麼是空指針異常,怎麼解決呢?(http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) –