alphiii
alphiii

Reputation: 1645

Calling fragment after recieved retrofit response (textview and button are null inside fragment)

I am learning Android and came across very strange behavior. I am using Retrofit2 as REST library in android (using asynchronous calls). I want to send authCode and TokenId as Google advise it on my server. When I check if user has set password I do a response. If I return code 206 means that user has not yet set password on my back-end server. So I want to start a fragment that user will enter password (I am also say that I defined LoginFragment and RegistrationFragment that both work on this Activity). But here is the trick, Fragment get called and when my onCreateView is executed there but TextView and Button has null value why is that? I assume that there is a problem since this is run on background thread but I may be mistaken. Is there a better way to achieve what I want?

Upvotes: 0

Views: 1754

Answers (2)

alphiii
alphiii

Reputation: 1645

I know my problem description does not point to the real problem that I found but still I will post it since maybe it will help someone. I my case it was problem in xml file I just removed android:fillViewport="true" or set it to false and it works.

Upvotes: 0

Narayan Acharya
Narayan Acharya

Reputation: 1499

This should probably fix it :

@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();
        }
    });

}

Note : I dont rexactly remember the exact parameters for the onViewCreated method but view is definitely there. So I guess this should work.

Upvotes: 1

Related Questions