gpt4 book ai didi

android - 在 Android 2.2 上运行时错误膨胀类 android.support.v4.app.fragment

转载 作者:行者123 更新时间:2023-11-30 03:15:51 25 4
gpt4 key购买 nike

我想在 Android Foryo 中添加对我的应用程序的支持。

但我似乎无法解决问题。我使用 Google 和 StackOverflow 做了很多研究。我唯一得到的是我必须使用 <android.support.v4.app.fragment而不是 <fragment使用支持库,但这也是行不通的!

每当我尝试运行该应用程序时,它就会崩溃,并且我会在 LogCat 中收到此错误。

android.view.InflateException: Binary XML file line #40: Error inflating class android.support.v4.app.fragment

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>

<LinearLayout
android:id="@+id/settings_fragment_layout"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFffffff"
android:orientation="vertical" >

<android.support.v4.app.fragment
android:id="@+id/settings_fragment_section"
android:name="com.example.SettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

</android.support.v4.widget.DrawerLayout>

Java

/**
*
*/
package com.example;

import android.app.TabActivity;
import android.os.Bundle;

/**
* @author
*
*/

public class MainActivity extends TabActivity {

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

SettingsFragment.java只是……

package com.example;

import android.os.Bundle;

import com.nozzha.support.v4.preference.PreferenceFragment;

public class SettingsFragment extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_settings);
}

}

LogCat

11-22 11:19:54.141: E/AndroidRuntime(1197): FATAL EXCEPTION: main
11-22 11:19:54.141: E/AndroidRuntime(1197): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: android.view.InflateException: Binary XML file line #40: Error inflating class android.support.v4.app.fragment
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.os.Looper.loop(Looper.java:123)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-22 11:19:54.141: E/AndroidRuntime(1197): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 11:19:54.141: E/AndroidRuntime(1197): at java.lang.reflect.Method.invoke(Method.java:521)
11-22 11:19:54.141: E/AndroidRuntime(1197): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-22 11:19:54.141: E/AndroidRuntime(1197): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-22 11:19:54.141: E/AndroidRuntime(1197): at dalvik.system.NativeStart.main(Native Method)
11-22 11:19:54.141: E/AndroidRuntime(1197): Caused by: android.view.InflateException: Binary XML file line #40: Error inflating class android.support.v4.app.fragment
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.createView(LayoutInflater.java:503)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-22 11:19:54.141: E/AndroidRuntime(1197): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.Activity.setContentView(Activity.java:1647)
11-22 11:19:54.141: E/AndroidRuntime(1197): at com.example.MainActivity.onCreate(MainActivity.java:19)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-22 11:19:54.141: E/AndroidRuntime(1197): ... 11 more
11-22 11:19:54.141: E/AndroidRuntime(1197): Caused by: java.lang.NoSuchMethodException: fragment(Context,AttributeSet)
11-22 11:19:54.141: E/AndroidRuntime(1197): at java.lang.Class.getMatchingConstructor(Class.java:660)
11-22 11:19:54.141: E/AndroidRuntime(1197): at java.lang.Class.getConstructor(Class.java:477)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.createView(LayoutInflater.java:475)
11-22 11:19:54.141: E/AndroidRuntime(1197): ... 22 more

最佳答案

那是因为您的 Activity 没有扩展基础 Activity FragmentActivity。如果您使用 Support Based Fragment,那么您的 Activity 必须扩展 FragmentActivity

http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html

关于android - 在 Android 2.2 上运行时错误膨胀类 android.support.v4.app.fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20144744/

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