I'm trying to pass data from an activity to a fragment. The onStart method from the activity calls the getUID
method inside the fragment and passes a String
, the fragment on the other end receives the String
and sets the text from a TextView
.
The problem is that the fragment is null by the time the activity calls the method in the fragment:
Stack Trace:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.project/com.test.project.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.test.project.Chat.getUID(android.os.Bundle)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) at android.app.ActivityThread.access$900(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5451) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.test.project.Chat.getUID(android.os.Bundle)' on a null object reference at com.test.project.MainActivity.onStart(MainActivity.java:178) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1238) at android.app.Activity.performStart(Activity.java:6340) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2397) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) at android.app.ActivityThread.access$900(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5451) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
MyClass myClass = new MyClass();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager.beginTransaction().replace(R.id.container, myClass).addToBackStack(null).commit();
mAuth = FirebaseAuth.getInstance();
}
...
protected void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null ) {
...
} else {
String uid = currentUser.getUid();
Chat chatFragment = (Chat)getSupportFragmentManager().findFragmentById(R.id.chat_layout);
Bundle bundle = new Bundle();
bundle.putString("uid", uid);
chatFragment.getUID(bundle);
This class below creates a ViewPager that populates the tabs in a tabLayout with ViewPagerAdapter.
MyClass.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView = inflater.inflate(R.layout.fragment_my_class, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.view_pager);
viewPager.setAdapter(new ViewPagerAdapter(getActivity().getSupportFragmentManager()));
tabLayout = (TabLayout) rootView.findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
ViewPagerAdapter
public ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
Log.e("error","Adapter");
childFragments = new Fragment[]{
new Chat(),
new Reminders(),
new Agenda()
};
}
@Override
public Fragment getItem(int position) {
Log.e("error", String.valueOf(position));
return childFragments[position];
}
@Override
public int getCount() {
return childFragments.length;
}
@Override
public CharSequence getPageTitle(int position){
String title = getItem(position).getClass().getName();
return title.subSequence(title.lastIndexOf(".") + 1, title.length());
}
Chat.java (fragment)
public class Chat extends Fragment {
private TextView text1;
public void getUID(Bundle uid){
String gotuid = uid.getString("uid");
text1.setText(gotuid);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_chat, container, false);
text1 = (TextView) rootView.findViewById(R.id.uidTextView);
return rootView;
}
}
I put some Log.i here and there and found out that the Chat.java fragment is created sometime after the MainActivity.java's onStart() is finished. How do I let the activity know when the Fragment is displayed/created so that the activity can send data to it?
Any help would be appreciated.