gpt4 book ai didi

android - 为什么我不能在 Android 中扩充我的 Fragments?

转载 作者:行者123 更新时间:2023-11-29 14:52:07 25 4
gpt4 key购买 nike

我有一个 Activity 将包含三个 fragment ,两个 fragment 具有 listView 布局,一个 fragment 具有 Relative 布局

这是 Activity 的 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>

这是主要 Activity 的 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;
}
}

所以我只是尝试加载空 fragment 以查看它们是否真的会加载,然后添加小部件和代码以将数据导入 fragment 。但是我收到一个错误提示 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thesis.menubook/com.thesis.menubook.MenuMain}:

有什么是我忽略的导致它无法加载空 fragment 的原因吗?

这是我的 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**

菜单类.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;
}
}

菜单 fragment .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;
}
}

以下是 fragment 的 xml 文件:

Activity 菜单类别

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

activity_menu_fragment

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

Activity 订单列表

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

最佳答案

您使用了错误的 Fragment 类。您正在使用支持库 fragment android.support.v4.app.Fragment。它需要 native 框架实现:android.app.Fragment

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

关于android - 为什么我不能在 Android 中扩充我的 Fragments?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14834719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com