为什么我在将数据从一个片段发送到另一个片段时出错
why i am getting error when sending data from one fragment to another
已阅读这些问题和许多其他问题,但没有人能给出简单的解释
send data from one fragment to another using bundle . i tried this. it's not working
Why can I not get my app to successfully send data from one fragment to another?
嗨,我对编程和 Whosebug 还很陌生
我创建了一个 MainActivity,其中有两个片段
<fragment
android:id="@+id/fragment1"
android:name="fragments.first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</fragment>
<fragment
android:id="@+id/fragment2"
android:name="fragments.second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</fragment>
在我的第一个片段中,我想在单击按钮时将数据发送到第二个框架
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String question = questions1.getText().toString();
String ans1 = answer1.getText().toString();
String ans2 = answer2.getText().toString();
String ans3 = answer3.getText().toString();
String ans4 = answer3.getText().toString();
Map<String,String> hashMap = new HashMap<String,String>();
hashMap.put("Answer 1",ans1);
hashMap.put("Answer 2",ans2);
hashMap.put("Answer 3",ans3);
hashMap.put("Answer 4",ans4);
Bundle bundle = new Bundle();
bundle.putString("Question1",question);
bundle.putString("Answer 1",ans1);
bundle.putString("Answer 2",ans2);
bundle.putString("Answer 3",ans3);
bundle.putString("Answer 4",ans4);
String correctAnswer= CorrectAnswer;
second mfragment=new second();
mfragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment1, mfragment).commit();
Toast.makeText(getActivity(),question,Toast.LENGTH_SHORT).show();
}
});
在第二个片段中
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();// i am receiving error here. what should i do?
String question = bundle.getString("Question1");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_second, container, false);
return root;
}
但收到错误
2021-05-04 03:19:28.676 23166-23166/com.example.mcqapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mcqapp, PID: 23166
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mcqapp/com.example.mcqapp.activities.AdminPost}: android.view.InflateException: Binary XML file line #25 in com.example.mcqapp:layout/activity_admin_post: Binary XML file line #25 in com.example.mcqapp:layout/activity_admin_post: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3556)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2216)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7948)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
Caused by: android.view.InflateException: Binary XML file line #25 in com.example.mcqapp:layout/activity_admin_post: Binary XML file line #25 in com.example.mcqapp:layout/activity_admin_post: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #25 in com.example.mcqapp:layout/activity_admin_post: Error inflating class fragment
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at fragments.second.onCreate(second.java:20)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2586)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:838)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1133)
at androidx.fragment.app.FragmentManagerImpl.addFragment(FragmentManagerImpl.java:1393)
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManagerImpl.java:3205)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:134)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:336)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1069)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1126)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.mcqapp.activities.AdminPost.onCreate(AdminPost.java:24)
at android.app.Activity.performCreate(Activity.java:7955)
at android.app.Activity.performCreate(Activity.java:7944)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3531)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
2021-05-04 03:19:28.677 23166-23166/com.example.mcqapp E/AndroidRuntime: 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:2216)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7948)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
我解决了。我从 activity 中删除了第二个片段,并将第一个片段转换为框架布局,因为数据重叠并以编程方式添加了第一个片段
<FrameLayout
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
getSupportFragmentManager().beginTransaction().add(R.id.fragment1,new first()).commit();
然后像这样在 createView 上编辑第二个片段:
Bundle bundle = getArguments();
if (bundle != null){
String question = bundle.getString("Question1");
String Question = question.toString();
textView.setText(Question);
我阅读了很多 Whosebug 问题并使用了框架布局,因为发生了重叠,对吗?
已阅读这些问题和许多其他问题,但没有人能给出简单的解释
send data from one fragment to another using bundle . i tried this. it's not working Why can I not get my app to successfully send data from one fragment to another?
嗨,我对编程和 Whosebug 还很陌生
我创建了一个 MainActivity,其中有两个片段
<fragment
android:id="@+id/fragment1"
android:name="fragments.first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</fragment>
<fragment
android:id="@+id/fragment2"
android:name="fragments.second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</fragment>
在我的第一个片段中,我想在单击按钮时将数据发送到第二个框架
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String question = questions1.getText().toString();
String ans1 = answer1.getText().toString();
String ans2 = answer2.getText().toString();
String ans3 = answer3.getText().toString();
String ans4 = answer3.getText().toString();
Map<String,String> hashMap = new HashMap<String,String>();
hashMap.put("Answer 1",ans1);
hashMap.put("Answer 2",ans2);
hashMap.put("Answer 3",ans3);
hashMap.put("Answer 4",ans4);
Bundle bundle = new Bundle();
bundle.putString("Question1",question);
bundle.putString("Answer 1",ans1);
bundle.putString("Answer 2",ans2);
bundle.putString("Answer 3",ans3);
bundle.putString("Answer 4",ans4);
String correctAnswer= CorrectAnswer;
second mfragment=new second();
mfragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment1, mfragment).commit();
Toast.makeText(getActivity(),question,Toast.LENGTH_SHORT).show();
}
});
在第二个片段中
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();// i am receiving error here. what should i do?
String question = bundle.getString("Question1");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_second, container, false);
return root;
}
但收到错误
2021-05-04 03:19:28.676 23166-23166/com.example.mcqapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mcqapp, PID: 23166
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mcqapp/com.example.mcqapp.activities.AdminPost}: android.view.InflateException: Binary XML file line #25 in com.example.mcqapp:layout/activity_admin_post: Binary XML file line #25 in com.example.mcqapp:layout/activity_admin_post: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3556)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2216)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7948)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
Caused by: android.view.InflateException: Binary XML file line #25 in com.example.mcqapp:layout/activity_admin_post: Binary XML file line #25 in com.example.mcqapp:layout/activity_admin_post: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #25 in com.example.mcqapp:layout/activity_admin_post: Error inflating class fragment
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at fragments.second.onCreate(second.java:20)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2586)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:838)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1133)
at androidx.fragment.app.FragmentManagerImpl.addFragment(FragmentManagerImpl.java:1393)
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManagerImpl.java:3205)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:134)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:336)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1069)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1126)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.mcqapp.activities.AdminPost.onCreate(AdminPost.java:24)
at android.app.Activity.performCreate(Activity.java:7955)
at android.app.Activity.performCreate(Activity.java:7944)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3531)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
2021-05-04 03:19:28.677 23166-23166/com.example.mcqapp E/AndroidRuntime: 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:2216)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7948)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
我解决了。我从 activity 中删除了第二个片段,并将第一个片段转换为框架布局,因为数据重叠并以编程方式添加了第一个片段
<FrameLayout
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
getSupportFragmentManager().beginTransaction().add(R.id.fragment1,new first()).commit();
然后像这样在 createView 上编辑第二个片段:
Bundle bundle = getArguments();
if (bundle != null){
String question = bundle.getString("Question1");
String Question = question.toString();
textView.setText(Question);
我阅读了很多 Whosebug 问题并使用了框架布局,因为发生了重叠,对吗?