0

我试图制作一个菜单来控制片段 - 屏幕。(就像在 Instagram 中一样),

但它崩溃了:

主要活动的代码:

public class All extends Activity {

    Button map;
    Button add;

    View MapFragment;
    View AddFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all);

        map=(Button)findViewById(R.id.map);
        add=(Button)findViewById(R.id.add);

        MapFragment=(View)findViewById(R.id.MapFragment);
        AddFragment=(View)findViewById(R.id.AddFragment);

        MapFragment.bringToFront();

        map.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                MapFragment.bringToFront();
            }
        });

        add.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                AddFragment.bringToFront();
            }
        });
    }

和日志猫:

09-29 19:14:11.765: E/ActivityThread(26360): Failed to inflate
09-29 19:14:11.765: E/ActivityThread(26360): android.view.InflateException: Binary XML file line #255: Error inflating class fragment
09-29 19:14:11.765: E/ActivityThread(26360):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-29 19:14:11.765: E/ActivityThread(26360):    at com.example.free.Add.onCreateView(Add.java:143)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:807)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1013)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1112)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.app.Activity.onCreateView(Activity.java:4664)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-29 19:14:11.765: E/ActivityThread(26360):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:275)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.app.Activity.setContentView(Activity.java:1867)
09-29 19:14:11.765: E/ActivityThread(26360):    at com.example.free.All.onCreate(All.java:26)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.app.Activity.performCreate(Activity.java:5008)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.app.ActivityThread.access$600(ActivityThread.java:142)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.os.Looper.loop(Looper.java:137)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.app.ActivityThread.main(ActivityThread.java:4931)
09-29 19:14:11.765: E/ActivityThread(26360):    at java.lang.reflect.Method.invokeNative(Native Method)
09-29 19:14:11.765: E/ActivityThread(26360):    at java.lang.reflect.Method.invoke(Method.java:511)
09-29 19:14:11.765: E/ActivityThread(26360):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-29 19:14:11.765: E/ActivityThread(26360):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-29 19:14:11.765: E/ActivityThread(26360):    at dalvik.system.NativeStart.main(Native Method)
09-29 19:14:11.765: E/ActivityThread(26360): Caused by: java.lang.IllegalArgumentException: Binary XML file line #255: Duplicate id 0x7f07001d, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
09-29 19:14:11.765: E/ActivityThread(26360):    at android.app.Activity.onCreateView(Activity.java:4669)
09-29 19:14:11.765: E/ActivityThread(26360):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
09-29 19:14:11.765: E/ActivityThread(26360):    ... 32 more
09-29 19:14:12.480: D/AndroidRuntime(26360): Shutting down VM
09-29 19:14:12.480: W/dalvikvm(26360): threadid=1: thread exiting with uncaught exception (group=0x40ed5300)
09-29 19:14:12.519: E/AndroidRuntime(26360): FATAL EXCEPTION: main
09-29 19:14:12.519: E/AndroidRuntime(26360): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.free/com.example.free.All}: android.view.InflateException: Binary XML file line #255: Error inflating class fragment
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.app.ActivityThread.access$600(ActivityThread.java:142)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.os.Looper.loop(Looper.java:137)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.app.ActivityThread.main(ActivityThread.java:4931)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at java.lang.reflect.Method.invokeNative(Native Method)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at java.lang.reflect.Method.invoke(Method.java:511)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at dalvik.system.NativeStart.main(Native Method)
09-29 19:14:12.519: E/AndroidRuntime(26360): Caused by: android.view.InflateException: Binary XML file line #255: Error inflating class fragment
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at com.example.free.Add.onCreateView(Add.java:143)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:807)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1013)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1112)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.app.Activity.onCreateView(Activity.java:4664)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:275)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.app.Activity.setContentView(Activity.java:1867)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at com.example.free.All.onCreate(All.java:26)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.app.Activity.performCreate(Activity.java:5008)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139)
09-29 19:14:12.519: E/AndroidRuntime(26360):    ... 11 more
09-29 19:14:12.519: E/AndroidRuntime(26360): Caused by: java.lang.IllegalArgumentException: Binary XML file line #255: Duplicate id 0x7f07001d, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.app.Activity.onCreateView(Activity.java:4669)
09-29 19:14:12.519: E/AndroidRuntime(26360):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
09-29 19:14:12.519: E/AndroidRuntime(26360):    ... 32 more

“添加”上的第 143 行:

@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            return inflater.inflate(R.layout.activity_add, container, false);
        }

有什么问题吗?

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".All"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.example.free.MainActivity"
        android:layout_marginBottom="40dp">

    </fragment>

    <fragment
        android:id="@+id/AddFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.example.free.Add"
        android:layout_marginBottom="40dp">

    </fragment>

    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/imageView1"
        android:text="Add" />

    <Button
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/imageView1"
        android:text="Map" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/divider" />

</RelativeLayout>

感谢你的协助!

编辑

现在我这样做:

Fragment MapFragment = getSupportFragmentManager().findFragmentById(R.id.MapFragment);
4

1 回答 1

1

您必须使用 FragmentActivity 而不是 Activity(如果您使用的是支持库)。

AddFragment=(查看)findViewById(R.id.AddFragment);

这是错的。阅读这篇文章,如何使用片段:http: //developer.android.com/reference/android/app/Fragment.html

顺便说一句,您不能将 Fragment 转换为 View。

于 2013-09-29T16:45:23.113 回答