I just created a new Basic Activity from the gallery of Android Studio and I can't figure out how to change the text of the fragment_first
. I've tried this, this, this and this but nothing works, I can't change the text of the fragment from the MainActivity.java
file. I always end up getting a "null reference" error, and it doesn't make sense to me.
Here are my files, they're basically the same as the Basic Activity template, but with a new method in the FirstFragment.java
that should change the text inside it.
------MainActivity.java------
public class MainActivity extends AppCompatActivity {
FirstFragment firstFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//Here's my problem.
firstFragment = (FirstFragment) getSupportFragmentManager().findFragmentById(R.id.firstfrag);
firstFragment.changeTextOfFragment("Stuff");
}
//onCreateOptionsMenu, onOptionsItemSelected...
}
------FirstFragment.java------
public class FirstFragment extends Fragment {
private View view;
private TextView textView;
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_first, container, false);
//On debug mode this works, it assigns the view to the textView variable
textView = view.findViewById(R.id.textview_first);
return view;
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavHostFragment.findNavController(FirstFragment.this)
.navigate(R.id.action_FirstFragment_to_SecondFragment);
}
});
}
// My method
public void changeTextOfFragment(String text) {
textView = view.findViewById(R.id.textview_first);
textView.setText(text);
}
}
And finally, here's the XML for the FirstFragment:
------FirstFragment.xml------
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/firstfrag"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstFragment">
<TextView
android:id="@+id/textview_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_first_fragment"
app:layout_constraintBottom_toTopOf="@id/button_first"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
.../>
</androidx.constraintlayout.widget.ConstraintLayout>
I think that the only possible logical reason as to why I'm referencing a null fragment it's because the fragment itself is finishing being created only after the MainActivity
is already done. If so, how can I be sure the fragment was finally created before trying to change its text? Should I set the TextView
of the FirstFragment
class on the onCreateView()
method or onViewCreated()
, since the button is being referenced there?