0

我正在尝试在布局中使用片段。我的 Android 应用程序面向 4.0.0 和更新版本,所以我没有使用 v4 支持库,而是使用内置的 Fragment 库。

我尝试了以下代码,但应用程序崩溃了。

这是主要活动:

package com.m3ph1st0s.vantium;

import android.os.Bundle;
import android.view.Window;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

public class AdminHomeActivity_wf extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.admin_home_wf);     
    Fragment fragment = new AH_MainFragment();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    transaction.replace(R.id.frame, fragment);
    transaction.commit();
  }

}

以其布局:

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="40dp"
    android:layout_height="180dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:contentDescription="@+string/desc"
    android:scaleType="fitXY"
    android:src="@drawable/a_home_side" />

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/imageView1"
    android:layout_toRightOf="@+id/imageView1" >

</FrameLayout>

片段类:

    package com.m3ph1st0s.vantium;
    import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

和片段布局:

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

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <LinearLayout
                android:id="@+id/IconUser"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/IconApps"
                android:layout_marginRight="71dp"
                android:layout_toLeftOf="@+id/IconApps"
                android:background="@drawable/contour"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:contentDescription="@+string/desc"
                    android:src="@drawable/a_home_user" />

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/a_home_user"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textSize="10sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/IconIntAcc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/IconApps"
                android:layout_alignLeft="@+id/IconUser"
                android:background="@drawable/contour"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView21"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:contentDescription="@+string/desc"
                    android:src="@drawable/a_home_intacc" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/a_home_intacc"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textSize="10sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/IconApps"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/IconUser"
                android:layout_centerHorizontal="true"
                android:background="@drawable/contour"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView12"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:contentDescription="@+string/desc"
                    android:src="@drawable/a_home_appmng" />

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/a_home_appmng"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textSize="10sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/IconAppWeb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/IconApps"
                android:layout_alignTop="@+id/IconIntAcc"
                android:background="@drawable/contour"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView5"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:contentDescription="@+string/desc"
                    android:src="@drawable/a_home_applis" />

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/a_home_applis"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textSize="10sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/IconTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/IconApps"
                android:layout_marginLeft="71dp"
                android:layout_toRightOf="@+id/IconApps"
                android:background="@drawable/contour"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView8"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:contentDescription="@+string/desc"
                    android:src="@drawable/a_home_time" />

                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/a_home_time"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textSize="10sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/IconIntBrows"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/IconTime"
                android:layout_alignTop="@+id/IconAppWeb"
                android:background="@drawable/contour"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView7"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:contentDescription="@+string/desc"
                    android:src="@drawable/a_home_intbrset" />

                <TextView
                    android:id="@+id/textView6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/a_home_intbrset"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textSize="10sp" />
            </LinearLayout>
        </RelativeLayout>

    </fragment>

我在 logcat 中得到错误:

      06-30 14:47:01.824: D/AndroidRuntime(2526): Shutting down VM
06-30 14:47:01.824: W/dalvikvm(2526): threadid=1: thread exiting with uncaught exception (group=0x40e0b930)
06-30 14:47:01.832: E/AndroidRuntime(2526): FATAL EXCEPTION: main
06-30 14:47:01.832: E/AndroidRuntime(2526): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.m3ph1st0s.vantium/com.m3ph1st0s.vantium.AdminHomeActivity_wf}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.os.Looper.loop(Looper.java:137)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at java.lang.reflect.Method.invokeNative(Native Method)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at java.lang.reflect.Method.invoke(Method.java:511)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at dalvik.system.NativeStart.main(Native Method)
06-30 14:47:01.832: E/AndroidRuntime(2526): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at com.m3ph1st0s.vantium.AH_MainFragment.onCreateView(AH_MainFragment.java:14)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.Fragment.performCreateView(Fragment.java:1695)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.Activity.onCreateView(Activity.java:4736)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at com.m3ph1st0s.vantium.AH_MainFragment.onCreateView(AH_MainFragment.java:14)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.Fragment.performCreateView(Fragment.java:1695)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.Activity.performStart(Activity.java:5113)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
06-30 14:47:01.832: E/AndroidRuntime(2526):     ... 11 more
06-30 14:47:01.832: E/AndroidRuntime(2526): Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f070023, tag null, or parent id 0x7f070013 with another fragment for null
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.app.Activity.onCreateView(Activity.java:4722)
06-30 14:47:01.832: E/AndroidRuntime(2526):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
06-30 14:47:01.832: E/AndroidRuntime(2526):     ... 29 more
06-30 14:47:03.629: I/Process(2526): Sending signal. PID: 2526 SIG: 9

我不明白有什么问题。有人可以帮忙吗?

4

2 回答 2

1

我找到了一个解决方案,尽管我不确定我是否理解它的工作原理:

我只是用片段的 XML 文件中的fragment标签替换了标签。也应该工作。RelativeLayoutLinearLayout

显然这是一个识别fragment类的问题。

于 2013-06-30T12:43:29.650 回答
0

原因是,如果您FragmentManager要替换/添加片段,则需要使用 aFrameLayout作为占位符。如果您使用片段,那么您无法替换/添加,这就是您崩溃的原因。

资源

于 2016-05-01T15:24:47.837 回答