1

我有一个活动,其中包含三个片段,两个具有listView布局,一个具有Relative layout.

这是活动的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id = "@+id/activity_main_large"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" xmlns:tools="http://schemas.android.com/tools">

     <fragment
         android:id="@+id/menu_category_fragment"
         android:name="com.thesis.menubook.MenuCategory"
         android:layout_width="123dp"
         android:layout_height="match_parent"
         tools:layout="@layout/activity_menu_category" />

     <fragment
         android:id="@+id/menu_fragment"
         android:name="com.thesis.menubook.MenuFragment"
         android:layout_width="0dip"
         android:layout_height="match_parent"
         android:layout_weight="0.35"
         tools:layout="@layout/activity_menu_fragment" />

     <fragment
         android:id="@+id/orderlist_fragment"
         android:name="com.thesis.menubook.OrderListFragment"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:layout_weight="0.08"
         tools:layout="@layout/activity_order_list" />

</LinearLayout>

这是主要活动的java文件

package com.thesis.menubook;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MenuMain extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.layout.activity_menu_main, menu);
        return true;
    }
}

在那里,我只是试图加载空片段以查看它们是否会实际加载,然后添加小部件和代码以将数据导入片段。但我收到一条错误消息java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thesis.menubook/com.thesis.menubook.MenuMain}:

有什么我忽略的原因是它无法加载空片段的原因吗?

这是我的 LogCat(更正类名后更新):

02-12 23:04:28.369: E/AndroidRuntime(1143): FATAL EXCEPTION: main
02-12 23:04:28.369: E/AndroidRuntime(1143): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thesis.menubook/com.thesis.menubook.MenuMain}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.app.ActivityThread.access$1500(ActivityThread.java:122)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.os.Looper.loop(Looper.java:132)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.app.ActivityThread.main(ActivityThread.java:4025)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at java.lang.reflect.Method.invoke(Method.java:491)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at dalvik.system.NativeStart.main(Native Method)
02-12 23:04:28.369: E/AndroidRuntime(1143): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:223)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.app.Activity.setContentView(Activity.java:1780)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at com.thesis.menubook.MenuMain.onCreate(MenuMain.java:15)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
02-12 23:04:28.369: E/AndroidRuntime(1143):     ... 11 more
02-12 23:04:28.369: E/AndroidRuntime(1143): Caused by: java.lang.ClassCastException: com.thesis.menubook.MenuCategory cannot be cast to android.app.Fragment
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.app.Fragment.instantiate(Fragment.java:493)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.app.Fragment.instantiate(Fragment.java:468)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.app.Activity.onCreateView(Activity.java:4132)
02-12 23:04:28.369: E/AndroidRuntime(1143):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)
02-12 23:04:28.369: E/AndroidRuntime(1143):     ... 20 more
02-12 23:04:28.905: D/dalvikvm(1143): GC_CONCURRENT freed 329K, 6% free 7390K/7815K, paused 12ms+20ms
02-12 23:04:34.180: I/Process(1143): Sending signal. PID: 1143 SIG: 9**

MenuCategory.java

package com.thesis.menubook;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MenuCategory extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.activity_menu_category, container, false);
            return view;
    }
}

MenuFragment.java

package com.thesis.menubook;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MenuFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_menu_fragment, container, false);
        return view;
    }
}

OrderListFragment.java

package com.thesis.menubook;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class OrderListFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_order_list, container, false);
        return view;
    }
}

以下是片段的 xml 文件:

活动菜单类别

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


</ListView>

活动菜单片段

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent" android:gravity="bottom|top" xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/hello_world"
        android:paddingTop="20dp" />

</RelativeLayout>

活动订单列表

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

    <!-- Name Label -->
    <TextView
        android:id="@+id/categoryname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="6dip"
        android:textSize="20sp"
        android:textStyle="bold" />
</LinearLayout>
4

6 回答 6

3

您使用了错误的 Fragment 类。您正在使用支持库片段android.support.v4.app.Fragment。它期望本地框架实​​现:android.app.Fragment.

如果您想使用支持库,以便在旧版本上使用,您的活动需要扩展android.support.v4.app.FragmentActivity. 然后您将使用该getSupportFragmentManager()方法获取 FragmentManager 并添加您的片段。

于 2015-02-16T17:37:33.120 回答
2

这是你的答案

Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.android.fragments.MenuCategory: make sure class name exists, is public, and has an empty constructor that is public

确保您有默认/空public构造函数,MenuCategory并确保它MenuCategory不是内部类,如果是,那么它应该是publicand static

于 2013-02-12T14:29:25.390 回答
1

您的项目中有MenuCategory, MenuFragment,OrderListFragment类吗?如果是这样,您是否将它们放在名为 的包中com.example.android.fragments?如果您有这些类,但您将它们放在自己的包中,则需要将 xml 布局中的包名称更改为您自己的,而不是 com.example 。

于 2013-02-12T14:29:33.320 回答
0

1) 您应该在 Anroidmanifest.xml 中将 minSdkVersion 的值更改为 11。2)检查你的mainactivity.xml中定义的fragment的name值是否正确,即packagename.Fragmentclassname。

于 2015-03-26T18:44:45.300 回答
0

答案就在错误消息中:

无法实例化片段 com.example.android.fragments.MenuCategory

您需要在您的项目中拥有一个完全限定名称为 com.example.android.fragments.MenuCategory 的类,它是 Fragment 的子类,并且具有无参数构造函数

编辑添加:

在布局的标签中:

<fragment
         android:id=blah blah blah
         android:name="com.thesis.menubook.MenuCategory"

...你命名一个Java类。在您的示例中,它是“com.thesis.menubook.MenuCategory”。正如我所说,该类必须存在于您的项目中,扩展 Fragment,并具有无参数构造函数(实际上,您根本不应该提供任何构造函数,因此它只有默认构造函数)。对于每个片段标签,你必须有这样一个类,对应每个 android:name 属性的值。

于 2013-02-12T14:36:18.437 回答
0

对我来说问题是别的,我使用的是导航组件,碰巧我没有添加id片段标签,也一定要升级到最新的 gradle 版本(我3.3.2的暂时是)

于 2019-03-08T15:56:11.657 回答