在android中:如何使用tab bar Fragment Activity调用另一个片段

时间:2014-05-08 13:10:09

标签: android android-fragments android-tabhost

在使用fragments.i的标签栏活动中有树标签,我需要在同一个标​​签中加载以下类Fragment。

public class FindFragmentActivity extends Fragment {

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

    View rootView = inflater.inflate(R.layout.findpartner_list_activity,container, false);
        return rootView;
        }
}

我已尝试使用以下代码调用片段:

FindFragmentActivity fragment = new FindFragmentActivity();
FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

fragmentTransaction.replace(R.layout.activity_mypartner_view,fragment);

fragmentTransaction.commit();

logcat的:

05-08 18:52:17.755: E/AndroidRuntime(4021): FATAL EXCEPTION: main
05-08 18:52:17.755: E/AndroidRuntime(4021): java.lang.IllegalArgumentException: No view found for id 0x7f030003 (com.tech.cf:layout/findpartner_list_activity) for fragment FindFragmentActivity{417a2390 #1 id=0x7f030003}
05-08 18:52:17.755: E/AndroidRuntime(4021):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
05-08 18:52:17.755: E/AndroidRuntime(4021):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-08 18:52:17.755: E/AndroidRuntime(4021):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-08 18:52:17.755: E/AndroidRuntime(4021):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
05-08 18:52:17.755: E/AndroidRuntime(4021):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
05-08 18:52:17.755: E/AndroidRuntime(4021):     at android.os.Handler.handleCallback(Handler.java:730)
05-08 18:52:17.755: E/AndroidRuntime(4021):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 18:52:17.755: E/AndroidRuntime(4021):     at android.os.Looper.loop(Looper.java:137)
05-08 18:52:17.755: E/AndroidRuntime(4021):     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-08 18:52:17.755: E/AndroidRuntime(4021):     at java.lang.reflect.Method.invokeNative(Native Method)
05-08 18:52:17.755: E/AndroidRuntime(4021):     at java.lang.reflect.Method.invoke(Method.java:525)
05-08 18:52:17.755: E/AndroidRuntime(4021):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-08 18:52:17.755: E/AndroidRuntime(4021):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-08 18:52:17.755: E/AndroidRuntime(4021):     at dalvik.system.NativeStart.main(Native Method)

崩溃的app.how我可以在标签栏内容中调用和加载。

修改

code:
   getFragmentManager().beginTransaction()
    .replace(R.id.layout_partnerlistview, new FindFragmentActivity())
    .commit();

Layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout_partnerlistview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/logo"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:orientation="horizontal" >
            .
            .
            .
    </LinearLayout>
    </RelativeLayout>

它也会导致同样的错误(找不到视图)。

1 个答案:

答案 0 :(得分:2)

Fragments不能被称为Activities,它们只能通过FragmentManager进行初始化,因此这段代码毫无意义:

Intent intent = new Intent(getActivity(),FindFragmentActivity.class);
getActivity().startActivity(intent);

Activity正在托管Fragments,因此您的意图应该包含对相应Activity的显式\隐式引用,并且在其内部onCreate您可以调用所需的片段。也可以链接FragmentTransaction's方法:

getFragmentManager().beginTransaction()
    .replace(R.id.<my container>, new FindFragmentActivity())
    .commit();

修改

第一次尝试是正确的(使用FragmentManager),但有一个小错误。对于replace方法的第一个参数,您应该不提供布局的ID,而是提供活动的现有布局中的视图ID(R.id.myView),它将托管您的片段。看一下here例如