1

我这样做是否正确,我试图有一个没有相对进展的嵌套片段。第一个 onCreateView 包含它的视图,但在 onCreateView 中,我实例化了一个新类,该类扩展了一个片段,并且还有自己的 onCreateView 和 xml。我现在遇到错误。我只是想VideoPlayerFragment在第一个主视图中显示。希望了解更多。

public static class DemoObjectFragment extends Fragment {

    public static final String ARG_OBJECT = "object";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.load_main_menu_activity, container, false);
        //Bundle args = getArguments();

       Fragment videoFragment = new VideoPlayerFragment();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.testLayout, videoFragment).commit();
       //((TextView) rootView.findViewById(android.R.id.text1)).setText(
             //   Integer.toString(args.getInt(ARG_OBJECT)));
        return rootView;
    }
}

视频播放器片段

公共类 VideoPlayerFragment 扩展片段 {

  public static final String ARG_OBJECT = "object";

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.id.testLayout, container, false);
      Bundle args = getArguments();
     //((TextView) rootView.findViewById(android.R.id.text1)).setText(
           //   Integer.toString(args.getInt(ARG_OBJECT)));
      return rootView;
  }

}

testing_fragments

<?xml version="1.0" encoding="utf-8"?>

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.jinisys.restoplusordering.VideoPlayerFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lommeregnerv2">

<LinearLayout
    android:id="@+id/testLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton" />

</LinearLayout>

 </fragment> 

日志猫:

07-15 19:27:30.391: E/FragmentManager(8218): Activity state:
07-15 19:37:44.885: E/AndroidRuntime(8654): FATAL EXCEPTION: main
07-15 19:37:44.885: E/AndroidRuntime(8654): android.content.res.Resources$NotFoundException: Resource ID #0x7f040039 type #0x12 is not valid
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2103)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.content.res.Resources.getLayout(Resources.java:852)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at com.jinisys.restoplusordering.VideoPlayerFragment.onCreateView(VideoPlayerFragment.java:17)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.support.v4.app.Fragment.performStart(Fragment.java:1481)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.view.View.measure(View.java:15181)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.view.View.measure(View.java:15181)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.view.View.measure(View.java:15181)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.view.Choreographer.doFrame(Choreographer.java:525)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.os.Handler.handleCallback(Handler.java:615)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.os.Looper.loop(Looper.java:137)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at java.lang.reflect.Method.invokeNative(Native Method)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at java.lang.reflect.Method.invoke(Method.java:511)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-15 19:37:44.885: E/AndroidRuntime(8654):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

再次感谢@Luksprog 的提示,我自己弄清楚了。因为我想要一个嵌套片段,所以我需要一个父 xml 来在下面添加这个代码。此代码使我能够在片段中添加另一个片段。

<FrameLayout 

            android:id="@+id/fragg"
            android:layout_weight="2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
于 2013-08-02T02:50:06.387 回答