在膨胀碎片时发现未找到类别

时间:2016-01-03 15:07:54

标签: java android xml android-fragments

我试图在我的主要活动中包含两个片段,但它会导致找不到类的异常问题

第一个片段类( SearchFragment.class

public class SearchFragment extends Fragment{

 public SearchFragment(){

 }
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.search_fragment, container, false);
 }

}

第一个片段xml( search_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
android:padding="5dp"
tools:context=".SearchFragment" >

<EditText
    android:id="@+id/etSearch"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/butFilter"
    android:background="@drawable/custom_form"
    android:ems="10"
    android:paddingRight="77dp" >
</EditText>

<Button
    android:id="@+id/butFilter"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dp"
    android:background="@drawable/custom_filter"
    android:text="" />

<Button
    android:id="@+id/butSearch"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/etSearch"
    android:layout_marginLeft="10dp"
    android:background="@drawable/custom_search"
    android:padding="5dp"
    android:text="" />

<Button
    android:id="@+id/butDelete"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="7dp"
    android:layout_toLeftOf="@+id/butSearch"
    android:background="@drawable/custom_delete"
    android:text="x" />

第二个片段类( ContentFragment.class

public class ContentFragment extends Fragment {

public ContentFragment(){

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.content_fragment, container, false);
}
}

第二个xml片段( content_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".ContentFragment" >

</RelativeLayout>

主xml文件( main.xml

<?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" >

<fragment
    android:id="@+id/fragment1"
    android:name="com.guru.alex.SearchFragment”"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

<fragment
    android:id="@+id/fragment2"
    android:name="com.guru.alex.ContentFragment”"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/fragment1" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SlidingDrawer
        android:id="@+id/slidingDrawer1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:content="@+id/content"
        android:handle="@+id/handle"
        android:orientation="horizontal"
        android:rotation="180" >

        <Button
            android:id="@+id/handle"
            android:layout_width="15dp"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:text=""
            android:visibility="invisible" />

        <RelativeLayout
            android:id="@+id/content"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#333"
            android:orientation="vertical"
            android:rotation="180" >

            <TextView
                android:id="@+id/tvCat"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:background="#f37c20"
                android:gravity="center"
                android:text="Category"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#333" />

            <ExpandableListView
                android:id="@+id/elvItem"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/tvCat"
                android:animateLayoutChanges="true"
                android:divider="@drawable/custom_divider"
                android:dividerHeight="0dp"
                android:groupIndicator="@drawable/custom_group_indicator"
                android:transcriptMode="normal" >
            </ExpandableListView>
        </RelativeLayout>
    </SlidingDrawer>

    <SlidingDrawer
        android:id="@+id/slidingDrawer2"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:content="@+id/content2"
        android:handle="@+id/handle2"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/handle2"
            android:layout_width="15dp"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:text=""
            android:visibility="invisible" />

        <RelativeLayout
            android:id="@+id/content2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imgUser"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginBottom="10dp"
                android:background="#ccc"
                android:contentDescription="User Profile Picture"
                android:src="@drawable/default_user" />

            <ExpandableListView
                android:id="@+id/elvSignIn"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/imgUser"
                android:animateLayoutChanges="true"
                android:dividerHeight="0dp"
                android:groupIndicator="@drawable/custom_group_indicator"
                android:indicatorLeft="180dp"
                android:transcriptMode="disabled" >
            </ExpandableListView>
        </RelativeLayout>
    </SlidingDrawer>
</FrameLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

尝试以下更改:

  1. MainActivity.java应扩展FragmentActivity

  2. 确保SearchFragmentContentFragment正在使用import android.support.v4.app.FragmentMainActivity.java正在使用import android.support.v4.app.FragmentActivity

  3. 您在main.xml中声明片段时使用了额外的双引号。同时将 android.name 更改为

  4. 例如:

    <fragment
        android:id="@+id/fragment1"
        class="com.guru.alex.SearchFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />
    
    <fragment
        android:id="@+id/fragment2"
        class="com.guru.alex.ContentFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/fragment1" />
    

答案 1 :(得分:0)

尝试删除android:name并在xml的fragment标签中使用class:并将完全限定的fragment类名称。希望它有所帮助。