我想将一个活动的数据传递给另一个片段

时间:2019-04-18 15:18:10

标签: java android fragment

我有一个活动,我想将数据从活动传递到片段。我的主要活动包含两个片段和一个按钮,我只需单击按钮,然后片段上显示的虚拟数据就可以了,我可以尝试很多次,但是每次遇到相同的错误时,“二进制XML文件第9行:   夸大课程片段时出错”,请帮助我

例外:

  

进程:com.sytechnology.fragmentdata,PID:12907   java.lang.RuntimeException:无法启动活动ComponentInfo {com.sytechnology.fragmentdata / com.sytechnology.fragmentdata.MainActivity}:   android.view.InflateException:二进制XML文件第9行:错误   膨胀类片段           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)           在android.app.ActivityThread.access $ 800(ActivityThread.java:151)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:135)           在android.app.ActivityThread.main(ActivityThread.java:5254)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)        原因:android.view.InflateException:二进制XML文件第9行:膨胀类片段的错误           在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)           在android.view.LayoutInflater.rInflate(LayoutInflater.java:806)           在android.view.LayoutInflater.inflate(LayoutInflater.java:504)           在android.view.LayoutInflater.inflate(LayoutInflater.java:414)           在android.view.LayoutInflater.inflate(LayoutInflater.java:365)           在android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)           在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)           在com.sytechnology.fragmentdata.MainActivity.onCreate(MainActivity.java:15)           在android.app.Activity.performCreate(Activity.java:5990)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)           在android.app.ActivityThread.access $ 800(ActivityThread.java:151)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:135)           在android.app.ActivityThread.main(ActivityThread.java:5254)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)        原因:java.lang.NullPointerException:尝试调用虚拟方法'java.lang.String   android.os.Bundle.getString(java.lang.String)'在空对象上   参考           在com.sytechnology.fragmentdata.FragementOne.onCreateView(FragementOne.java:32)           在android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)           在android.support.v4.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManager.java:1689)           在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1431)           在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1684)           在android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1930)           在android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3745)           在android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)           在android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:405)

此代码是片段的数据传递活动

button =findViewById(R.id.mainButtonId);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Bundle bundle= new Bundle();

bundle.putString("demo","dummy data");

FragementOne fragementOne= new FragementOne();

fragementOne.setArguments(bundle);
       //getSupportFragmentManager().beginTransaction().replace(R.id.mainFragme ntOn,fragementOne).commit();
}
});

这是我从活动中接收数据的片段代码

TextView textView;

public FragementOne()
{

}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View rootView=inflater.inflate(R.layout.fragment_one,container,false);
    textView=(TextView) rootView.findViewById(R.id.fregmentOneText);

        String name=this.getArguments().getString("demo");


 textView.setText(name);
 return rootView;

"Here is My XML of Fragment"


 <?xml version="1.0" encoding="utf-8"?>
 <FrameLayout
 xmlns:android="http://schemas.android.com/apk/res/android"  


android:layout_width="match_parent"
android:background="@color/colorAccent"
android:id="@+id/mainFragmentOn"
android:layout_height="match_parent">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fregmentOneText"
android:text="fregmanetOn"
/>
</FrameLayout>

这是我的主要活动XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<fragment
android:layout_width="0px"
android:layout_height="300dp"
android:layout_weight="1"
android:name="com.sytechnology.fragmentdata.FragementOne"
android:id="@+id/fragment1"
/>
<fragment
    android:layout_width="0px"
    android:layout_height="300dp"
    android:id="@+id/fragment2"
    android:name="com.sytechnology.fragmentdata.FragmentTwo"
    android:layout_weight="1"
    />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mainButtonId"
android:text="click"
        android:background="@color/colorAccent"

        />


</LinearLayout>

0 个答案:

没有答案