0

我正在尝试使用简单片段


我在屏幕上收到日志错误

解决它们的任何想法


LM_Fragement.java

public class LM_Fragement extends Fragment{
   @Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {
      /**
       * Inflate the layout for this fragment
       */
      return inflater.inflate(
      R.layout.lm_fragement, container, false);
   }
}

PM_Fragement.java

public class PM_Fragement extends Fragment{
   @Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {
      /**
       * Inflate the layout for this fragment
       */
      return inflater.inflate(
      R.layout.pm_fragment, container, false);
   }
}

MainActivity.java

public class MainActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      Configuration config = getResources().getConfiguration();

      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = 
      fragmentManager.beginTransaction();

      /**
      * Check the device orientation and act accordingly
      */
      if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
         /**
         * Landscape mode of the device
         */
          LM_Fragement ls_fragment = new LM_Fragement();
         fragmentTransaction.replace(android.R.id.content, ls_fragment);
      }else{
         /**
         * Portrait mode of the device
         */
          PM_Fragement pm_fragment = new PM_Fragement();
         fragmentTransaction.replace(android.R.id.content, pm_fragment);
      }
      fragmentTransaction.commit();
   }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

   <fragment
   android:name="com.example.fragments"
   android:id="@+id/lm_fragment"
   android:layout_weight="1"
   android:layout_width="0dp"
   android:layout_height="match_parent" />

   <fragment
   android:name="com.example.fragments"
   android:id="@+id/pm_fragment"
   android:layout_weight="2"
   android:layout_width="0dp"
   android:layout_height="match_parent" />

</LinearLayout>

日志::

10-13 19:36:11.917: I/dalvikvm(652): Could not find method com.example.fragmentstutorialpoint.MainActivity.getFragmentManager, referenced from method com.example.fragmentstutorialpoint.MainActivity.onCreate
10-13 19:36:11.966: E/AndroidRuntime(652): FATAL EXCEPTION: main
10-13 19:36:11.966: E/AndroidRuntime(652): java.lang.NoSuchMethodError: com.example.fragmentstutorialpoint.MainActivity.getFragmentManager
10-13 19:36:11.966: E/AndroidRuntime(652):  at com.example.fragmentstutorialpoint.MainActivity.onCreate(MainActivity.java:22)
10-13 19:36:11.966: E/AndroidRuntime(652):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-13 19:36:11.966: E/AndroidRuntime(652):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-13 19:36:11.966: E/AndroidRuntime(652):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-13 19:36:11.966: E/AndroidRuntime(652):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-13 19:36:11.966: E/AndroidRuntime(652):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-13 19:36:11.966: E/AndroidRuntime(652):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 19:36:11.966: E/AndroidRuntime(652):  at android.os.Looper.loop(Looper.java:123)
10-13 19:36:11.966: E/AndroidRuntime(652):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-13 19:36:11.966: E/AndroidRuntime(652):  at java.lang.reflect.Method.invokeNative(Native Method)
10-13 19:36:11.966: E/AndroidRuntime(652):  at java.lang.reflect.Method.invoke(Method.java:507)
10-13 19:36:11.966: E/AndroidRuntime(652):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-13 19:36:11.966: E/AndroidRuntime(652):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-13 19:36:11.966: E/AndroidRuntime(652):  at dalvik.system.NativeStart.main(Native Method)

我该如何解决这些错误......

有任何想法吗

谢谢

4

1 回答 1

1

您是否在 3.0 之前的项目中使用支持库?

如果是这样,请getSupportFragmentManager改用

于 2013-10-13T14:13:15.713 回答