扩展类片段时出错(Tabhost)

时间:2013-03-22 13:10:04

标签: android android-layout android-fragments inflate-exception

这里我正在实现一个显示tabhost的活动。但我得到错误,因为错误膨胀二进制xml ....我在这个论坛搜索了类似的答案,但没有人帮助我? 这是我的活动课


package com.example.iolcalci;

import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.app.Activity;

public class Menu extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        TabHost mTab=(TabHost)findViewById(android.R.id.tabhost);
        mTab.setup();

        TabSpec specs=mTab.newTabSpec("tag1");
        specs.setContent(R.id.srkt_fragment);
        specs.setIndicator("SRK/T");
        mTab.addTab(specs);

        specs=mTab.newTabSpec("tag2");
        specs.setContent(R.id.srk2_fragment);
        specs.setIndicator("SRK II");
        mTab.addTab(specs);

        specs=mTab.newTabSpec("tag3");
        specs.setContent(R.id.holl_fragment);
        specs.setIndicator("HOLLADAY");
        mTab.addTab(specs);

        specs=mTab.newTabSpec("tag4");
        specs.setContent(R.id.bink_fragment);
        specs.setIndicator("BINKHORST");
        mTab.addTab(specs);
    }

}

这是我的xml代码


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

<TabHost xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:ignore="HardcodedText" xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="160dp"
                android:layout_weight="0.05"
                android:background="@color/Black" >

                <TextView
                    android:id="@+id/al"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/k1"
                    android:layout_alignBottom="@+id/k1"
                    android:layout_alignLeft="@+id/k2"
                    android:text="@string/AL"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <EditText
                    android:id="@+id/al_editText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/al"
                    android:layout_alignBottom="@+id/al"
                    android:layout_alignLeft="@+id/k2_editText"
                    android:layout_alignRight="@+id/k2_editText"
                    android:background="@drawable/editbg"
                    android:ems="10"
                    android:enabled="true"
                    android:inputType="numberDecimal"
                    android:scrollHorizontally="true"
                    android:singleLine="true"
                    android:textColor="@color/White"
                    android:textSize="15sp"
                    android:visibility="visible"
                    android:width="100dp" />

                <TextView
                    android:id="@+id/k1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="46dp"
                    android:layout_marginTop="27dp"
                    android:layout_toRightOf="@+id/al_editText"
                    android:text="@string/K1"
                    android:textColor="@color/white"
                    android:textSize="15sp" />

                <EditText
                    android:id="@+id/k2_editText"
                    android:layout_width="65dp"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/k2"
                    android:layout_alignBottom="@+id/k2"
                    android:layout_marginLeft="14dp"
                    android:layout_toRightOf="@+id/k2"
                    android:background="@drawable/editbg"
                    android:ems="10"
                    android:enabled="true"
                    android:inputType="numberDecimal"
                    android:scrollHorizontally="true"
                    android:singleLine="true"
                    android:textColor="@color/White"
                    android:textSize="15sp"
                    android:visibility="visible"
                    android:width="100dp" >

                    <requestFocus />
                </EditText>

                <TextView
                    android:id="@+id/k2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/al_editText"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="22dp"
                    android:text="@string/K2"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/al_const"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/k2_editText"
                    android:layout_alignLeft="@+id/k1"
                    android:text="@string/Rx"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/dr"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/k2_editText"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="17dp"
                    android:layout_toLeftOf="@+id/k1"
                    android:ems="10"
                    android:gravity="center"
                    android:text="@string/dr"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <EditText
                    android:id="@+id/dr_editText"
                    android:layout_width="65dp"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/dr"
                    android:layout_alignBottom="@+id/dr"
                    android:layout_toRightOf="@+id/dr"
                    android:background="@drawable/editbg"
                    android:ems="10"
                    android:inputType="numberDecimal"
                    android:singleLine="true"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/al_metric"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/k2_editText"
                    android:layout_alignTop="@+id/k1"
                    android:layout_toLeftOf="@+id/al_const"
                    android:layout_toRightOf="@+id/al_editText"
                    android:gravity="left|center_vertical"
                    android:text="mm"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/White" />

                <EditText
                    android:id="@+id/k1_editText"
                    android:layout_width="65dp"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/k1"
                    android:layout_alignBottom="@+id/k1"
                    android:layout_marginLeft="19dp"
                    android:layout_toRightOf="@+id/k1"
                    android:background="@drawable/editbg"
                    android:ems="10"
                    android:enabled="true"
                    android:inputType="numberDecimal"
                    android:scrollHorizontally="true"
                    android:singleLine="true"
                    android:textColor="@color/White"
                    android:textSize="15sp"
                    android:visibility="visible"
                    android:width="100dp" />

                <EditText
                    android:id="@+id/al_const_editText"
                    android:layout_width="65dp"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/al_const"
                    android:layout_alignBottom="@+id/al_const"
                    android:layout_toRightOf="@+id/al_const"
                    android:background="@drawable/editbg"
                    android:ems="10"
                    android:enabled="true"
                    android:inputType="numberDecimal"
                    android:scrollHorizontally="true"
                    android:singleLine="true"
                    android:textColor="@color/White"
                    android:textSize="15sp"
                    android:visibility="visible"
                    android:width="100dp" />

                <EditText
                    android:id="@+id/k1_metric"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/al_metric"
                    android:layout_alignTop="@+id/k1"
                    android:layout_toRightOf="@+id/k1_editText"
                    android:gravity="left|top"
                    android:inputType="text"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/White" />

                <EditText
                    android:id="@+id/k2_metric"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/k2_editText"
                    android:layout_alignTop="@+id/al_const"
                    android:layout_toRightOf="@+id/k2_editText"
                    android:inputType="text"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/White" />

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/dr_editText"
                    android:layout_alignRight="@+id/al_const"
                    android:layout_alignTop="@+id/dr"
                    android:layout_toRightOf="@+id/k1"
                    android:text="D"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/white" />
            </RelativeLayout>

            <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:background="@color/Black" >

            <fragment
                android:id="@+id/bink_fragment"
                android:name="com.example.iolcalci.Binkhorst_x"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                 />

            <fragment android:name="com.example.iolcalci.Srkt_x"
                android:id="@+id/srkt_fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
            <fragment android:name="com.example.iolcalci.Holladay_x"
                android:id="@+id/holl_fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
            <fragment android:name="com.example.iolcalci.Srk2_x"
                android:id="@+id/srk2_fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </FrameLayout>


    </LinearLayout>

</TabHost>
</RelativeLayout>

这是我的logcat报告


03-22 18:30:46.869: D/AndroidRuntime(364): Shutting down VM
03-22 18:30:46.869: W/dalvikvm(364): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-22 18:30:46.894: E/AndroidRuntime(364): FATAL EXCEPTION: main
03-22 18:30:46.894: E/AndroidRuntime(364): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iolcalci/com.example.iolcalci.Menu}: android.view.InflateException: Binary XML file line #236: Error inflating class fragment
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.os.Looper.loop(Looper.java:123)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-22 18:30:46.894: E/AndroidRuntime(364):  at java.lang.reflect.Method.invokeNative(Native Method)
03-22 18:30:46.894: E/AndroidRuntime(364):  at java.lang.reflect.Method.invoke(Method.java:507)
03-22 18:30:46.894: E/AndroidRuntime(364):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-22 18:30:46.894: E/AndroidRuntime(364):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-22 18:30:46.894: E/AndroidRuntime(364):  at dalvik.system.NativeStart.main(Native Method)
03-22 18:30:46.894: E/AndroidRuntime(364): Caused by: android.view.InflateException: Binary XML file line #236: Error inflating class fragment
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-22 18:30:46.894: E/AndroidRuntime(364):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.app.Activity.setContentView(Activity.java:1657)
03-22 18:30:46.894: E/AndroidRuntime(364):  at com.example.iolcalci.Menu.onCreate(Menu.java:13)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-22 18:30:46.894: E/AndroidRuntime(364):  ... 11 more
03-22 18:30:46.894: E/AndroidRuntime(364): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.iolcalci-2.apk]
03-22 18:30:46.894: E/AndroidRuntime(364):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
03-22 18:30:46.894: E/AndroidRuntime(364):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
03-22 18:30:46.894: E/AndroidRuntime(364):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.view.LayoutInflater.createView(LayoutInflater.java:471)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
03-22 18:30:46.894: E/AndroidRuntime(364):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
03-22 18:30:46.894: E/AndroidRuntime(364):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
03-22 18:30:46.894: E/AndroidRuntime(364):  ... 23 more

我不知道错误在哪里,因为它没有在日志文件中显示原因。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

如果您在布局中使用fragment标记,请使用class属性指向片段的类:

<fragment
     android:id="@+id/bink_fragment"
     class="com.example.iolcalci.Binkhorst_x"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
/>