java - 膨胀 fragment 时发现类未找到异常

标签 java android xml android-fragments

我试图在我的主 Activity 中包含两个 fragment ,但它导致类未找到异常问题

第一个 fragment 类(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);
 }

}

fragment 的第一个 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" />

第二个 fragment 类(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 fragment (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>

最佳答案

尝试以下更改:

  1. MainActivity.java 应扩展 FragmentActivity

  2. 确保 SearchFragmentContentFragment 使用 import android.support.v4.app.Fragment 并且 MainActivity.java 使用 import android.support.v4.app.FragmentActivity

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

例如:

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

关于java - 膨胀 fragment 时发现类未找到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34577972/

相关文章:

java - 使用 JAXP(JEE6) 解析简单的 XML 文档

xml - 在哪里保存大型博客文章数据 - 在文件或数据库中?

java - JFrame布局: Header and 2 Text Areas

java - 获取双SIM卡电话记录(通话记录来自Sim卡)

android - 我的启动画面图像总是出现一些圆白角

xml - 使用 xmlstarlet 或 xmllint 获取属性值

java - 部署时使用 RESTEasy Multipart 时出错

java - 如何在 JavaFX 中触发 Textfield 监听器?

java - Selenium 安装障碍 "importfirefoxdriver"

java - Android MediaRecorder/Player无法录制或无法播放