私はAndroidを学習していて、非常に奇妙な動作を経験しました。私はアンドロイドでRESTライブラリとしてRetrofit2を使用しています(非同期呼び出しを使用しています)。私はauthcodeとTokenIdを私のサーバー上でGoogleに通知するように送りたい。ユーザーがパスワードを設定しているかどうかを確認すると、私は応答をします。私がコード206を返すと、ユーザはまだ私のバックエンドサーバにパスワードを設定していないことを意味します。 私は、ユーザーがパスワードを入力するフラグメントを開始したいと思います(このアクティビティで動作するLoginFragmentとRegistrationFragmentを定義したとも言えます)。しかし、ここではトリックですが、フラグメントが呼び出され、onCreateViewがそこで実行されてもTextViewとButtonにnull値があるのはなぜですか?これはバックグラウンドスレッドで実行されているので問題があると思われますが、間違いかもしれません。私が欲しいものを達成する良い方法はありますか?受信したレトロフィット応答の後でフラグメントを呼び出す(テキストビューとボタンがフラグメント内でヌル)
私のレトロフィットコール:
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あるを拡張します
onCreateViewでビューを膨張させて返すだけで、フラグメントのonViewCreatedメソッド内の他のすべてのボタンとテキストビューの処理を行うことができます –
それについて考えず、試してみましょう。 – alphiii
[NullPointerExceptionとは何か、それを修正するにはどうすればいいですか?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) ) –