现在我正在尝试将数据从一个片段传递到另一个片段方法。
例如,
片段一个
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//DB Connection
...
...
values_description = stringArrayList_description.toArray(new String[stringArrayList_description.size()]);
Fragmenttwo frag_two = new Fragmenttwo();
Bundle args = new Bundle();
args.putString("data","data12344");
frag_two.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.text_view,frag_two);
//Toast.makeText(getActivity(),"position"+parent.getItemAtPosition(position),Toa st.LENGTH_SHORT).show();
}
});
第二片段
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_listview_two, container, false);
textView = (TextView)view.findViewById(R.id.text_view);
Bundle bundle = this.getArguments();
value = bundle.getString("data","nulll");
setData(value);
return view;
}
public void setData(String data){
textView.setText(data);
}
但我无法从第二个碎片接收数据,它会抛出错误,
04-07 11:24:03.887 2937-2937/com.ats.test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ats.test, PID: 2937
android.view.InflateException: Binary XML file line #13: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at fragments.Callhistory_Fragment.onCreateView(Callhistory_Fragment.java:35)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1453)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at fragments.Fragmenttwo.onCreateView(Fragmenttwo.java:27)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
at android.app.Activity.onCreateView(Activity.java:4877)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at fragments.Callhistory_Fragment.onCreateView(Callhistory_Fragment.java:35)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1453)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
片段处理另一个片段(fragmentone和fragmenttwo)
<fragment
android:layout_width="206dp"
android:layout_height="match_parent"
android:id="@+id/fragment1"
android:layout_weight="1"
android:name="fragments.Fragmentone"
android:layout_gravity="left|center_vertical"></fragment>
<fragment
android:layout_width="164dp"
android:layout_height="match_parent"
android:id="@+id/fragment2"
android:layout_weight="1"
android:name="fragments.Fragmenttwo"
android:layout_gravity="right|center_vertical"
></fragment>
</FrameLayout>
R.Layout.fragment_listview_two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f467f7">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_view"
android:text="description \n here"
android:textColor="#000000"
android:textAppearance="?android:textAppearanceLarge"/>
</LinearLayout>
请指导我,我不知道我错在哪里。
答案 0 :(得分:0)
试一试!!!!
在
添加bundle!= nullheight: auto !important
答案 1 :(得分:0)
试试这个..
在你的FromFragment:
Fragment fragment = new Fragment(); // replace your custom fragment class
Bundle bundle = new Bundle();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
bundle.putString("key","value"); // use as per your need
fragment.setArguments(bundle);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(viewID,fragment);
fragmentTransaction.commit();
在自定义片段类
中Bundle mBundle = new Bundle();
mBundle = getArguments();
mBundle.getString(key);
// key必须与第一个片段中给出的相同
答案 2 :(得分:0)
你需要像这样提交你的片段:
getFragmentManager().beginTransaction().add(R.id.text_view,frag_two).commit();
答案 3 :(得分:0)
您可以将任何数据简单地传递给第二个片段的构造函数。
String text = "test";
SecondFragment fragment = new SecondFragment(text);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
然后在SecondFragment
String receivingText;
public SecondFragment(String text){
this.receivingText = text;
}