I'm learning fragments communication with each other. However, I have encountered a problem and I do not know how to solve it. I have tons tons of researches on google and didn't found anything so i hope you can help me . This is my code :
Activity :
public class NextActivity extends AppCompatActivity {
public static final String EXTRA_BUTTON_TAG =
"com.example.elieazoury.testfragments.NextActivity";
private NextFragment nextFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
}
@Override
public void onResume() {
super.onResume();
// 3 - Call update method here because we are sure that DetailFragment is visible
nextFragment=new NextFragment();
this.updateDetailFragmentTextViewWithIntentTag();
}
private void updateDetailFragmentTextViewWithIntentTag(){ nextFragment.updateTextView();
}
}
and the fragment :
public class NextFragment extends Fragment {
private TextView textView;
public NextFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_next, container, false);
textView = view.findViewById(R.id.texttt);
return (view);
}
public void updateTextView( ) {
textView.setText("literally anything");
}
}
XML :
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:name="com.example.elieazoury.testfragments.NextFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="260dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/fragm"/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.elieazoury.testfragments.NextFragment">
<TextView
android:layout_width="wrap_content"
android:id="@+id/texttt"
android:layout_height="wrap_content"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
the log crash :
com.example.elieazoury.testfragments E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.elieazoury.testfragments, PID: 8727 java.lang.RuntimeException: Unable to resume activity {com.example.elieazoury.testfragments/com.example.elieazoury.testfragments.NextActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.v4.app.FragmentActivity.findViewById(int)' on a null object reference at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3581) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3621) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2862) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.v4.app.FragmentActivity.findViewById(int)' on a null object reference at com.example.elieazoury.testfragments.NextFragment.updateTextView(NextFragment.java:38) at com.example.elieazoury.testfragments.NextActivity.updateDetailFragmentTextViewWithIntentTag(NextActivity.java:42) at com.example.elieazoury.testfragments.NextActivity.onResume(NextActivity.java:31) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1355) at android.app.Activity.performResume(Activity.java:7117) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3556) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3621) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2862) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)