Answer the question
In order to leave comments, you need to log in
Why is Attempt to invoke virtual method setArguments() on a null object reference thrown?
Good day!
I study android and try to transfer information from one fragment to another.
Implemented interface:
public interface DataPassListener {
void passData(String data);
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
if(context instanceof DataPassListener) {
dataPassListener = (DataPassListener) context;
} else {
throw new RuntimeException(context.toString() + " must implement dataPassListener");
}
}
public class UserCreationActivity extends AppCompatActivity implements DataPassListener {
@Override
public void passData(String data) {
Log.d(LOG, "Получили сообщение в UserCreationActivity: "+ data);
fragment_2 = (UserCreationActivityFragment_2) this.getSupportFragmentManager().findFragmentById(R.id.secondFragmentUserCreation);
Bundle bundle = new Bundle();
bundle.putString("age",data);
fragment_2.setArguments(bundle);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_2_user_creation, container, false);
String age = null;
if (getArguments() != null) {
age = getArguments().getString("age");
}
Log.d(LOG, "Возраст " + age);
}
java.lang.NullPointerException: Attempt to invoke virtual method 'void info.md7.lfk.fragments.UserCreationActivityFragment_2.setArguments(android.os.Bundle)' on a null object reference
Answer the question
In order to leave comments, you need to log in
Obviously
(UserCreationActivityFragment_2) this.getSupportFragmentManager().findFragmentById(R.id.secondFragmentUserCreation);
Returns null.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question