Reputation: 1759
MainActivity.java
public class MainActivity extends AppCompatActivity {
SampleData data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShowFragment showFragment = (ShowFragment) getSupportFragmentManager().findFragmentById(R.id.showfragment);
data = new SampleData("ABCD", "20201031");
Bundle bundle = new Bundle();
bundle.putParcelable("key", data);
showFragment.setArguments(bundle);
}
}
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/showfragment"
android:name="com.example.test2.ShowFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
ShowFragment.java
public class ShowFragment extends Fragment {
SampleData sampleData;
TextView textView;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
sampleData = bundle.getParcelable("key");
Log.d("CHECK", "data : " + sampleData.title);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container2,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_show, container2, false);
textView = rootView.findViewById(R.id.title);
textView.setText(sampleData.title);
return rootView;
}
}
ERROR MESSAGE
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test2/com.example.test2.MainActivity}: android.view.InflateException: Binary XML file line #15 in com.example.test2:layout/activity_main: Binary XML file line #15 in com.example.test2:layout/activity_main: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.view.InflateException: Binary XML file line #15 in com.example.test2:layout/activity_main: Binary XML file line #15 in com.example.test2:layout/activity_main: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #15 in com.example.test2:layout/activity_main: Error inflating class fragment
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference
I want to pass data from activity to fragment using fragment in XML ( ) but i can't get data.
If i pass the data from activity to fragment. i just get ERROR(null object reference) at getParcelable...
i dont know what's wrong.
when pass the data using fragment in xml, why i cant get data? tell me why please
when pass data from activity to fragment in xml, how can i get data using getArgument?
Upvotes: 0
Views: 219
Reputation: 898
I want to pass data from activity to fragment
Why? The fragment can always access the activity by calling requireActivity()
What data do you actually want to pass? There is probably another and even better way to get your data to the fragment. You could have a getter in the activity, a setter in the fragment or even better work with ViewModels.
Update
Simple option:
in your fragment, add:
public void setTitle(String title) {
Log.d("CHECK", "data : " + title);
}
In your Activity,
ShowFragment showFragment = ...;
showFragment.setTitle("Test");
Upvotes: 1