从布局 android-support-v4 膨胀片段时出错

发布于 2024-12-22 18:31:55 字数 6218 浏览 1 评论 0原文

我在 2.2 平台上处理片段时遇到问题。我导入了 android-support-v4.jar 库来支持片段。
我尝试将目标更改为 Honeycomb 3.0,并且代码有效。所以,我想知道是否存在一种在本机平台上处理片段的方法以及在以前的版本中使用不同的方法。

这是我的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nsy.testefragmentos"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".TesteFragmentosActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            <activity android:name="FragmentoLista" />
            <activity android:name="FragmentoDetalhe" />
    </application>
</manifest>

这是我的主类

package com.nsy.testefragmentos;

import android.app.Activity;
import android.os.Bundle;

public class TesteFragmentosActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

最后,从 main.xml 文件调用的片段:

package com.nsy.testefragmentos;

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

public class FragmentoLista extends ListFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);         
    }   
}

当我运行时,以下错误出现在logCat:

12-24 11:46:16.708: E/AndroidRuntime(395): FATAL EXCEPTION: main
12-24 11:46:16.708: E/AndroidRuntime(395): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nsy.testefragmentos/com.nsy.testefragmentos.TesteFragmentosActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.os.Looper.loop(Looper.java:123)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-24 11:46:16.708: E/AndroidRuntime(395):  at java.lang.reflect.Method.invokeNative(Native Method)
12-24 11:46:16.708: E/AndroidRuntime(395):  at java.lang.reflect.Method.invoke(Method.java:521)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-24 11:46:16.708: E/AndroidRuntime(395):  at dalvik.system.NativeStart.main(Native Method)
12-24 11:46:16.708: E/AndroidRuntime(395): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.Activity.setContentView(Activity.java:1647)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.nsy.testefragmentos.TesteFragmentosActivity.onCreate(TesteFragmentosActivity.java:12)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-24 11:46:16.708: E/AndroidRuntime(395):  ... 11 more
12-24 11:46:16.708: E/AndroidRuntime(395): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.nsy.testefragmentos-1.apk]
12-24 11:46:16.708: E/AndroidRuntime(395):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
12-24 11:46:16.708: E/AndroidRuntime(395):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
12-24 11:46:16.708: E/AndroidRuntime(395):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.createView(LayoutInflater.java:466)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
12-24 11:46:16.708: E/AndroidRuntime(395):  ... 20 more
12-24 11:46:21.828: I/Process(395): Sending signal. PID: 395 SIG: 9

我真的不知道我做错了什么=/有人可以帮助我吗? thnkx

I'm having troubles working with fragments to the 2.2 platform. I imported the android-support-v4.jar library to support fragments.
I tried to change the target to Honeycomb 3.0, and the code works. So, I wanna know if there exists a way to work with fragments on a native platform and a different way to do that with prior versions.

Here is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nsy.testefragmentos"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".TesteFragmentosActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            <activity android:name="FragmentoLista" />
            <activity android:name="FragmentoDetalhe" />
    </application>
</manifest>

And here is my main class

package com.nsy.testefragmentos;

import android.app.Activity;
import android.os.Bundle;

public class TesteFragmentosActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

And finally, the fragment called from the main.xml file:

package com.nsy.testefragmentos;

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

public class FragmentoLista extends ListFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);         
    }   
}

when I run, the follow error appears in the logCat:

12-24 11:46:16.708: E/AndroidRuntime(395): FATAL EXCEPTION: main
12-24 11:46:16.708: E/AndroidRuntime(395): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nsy.testefragmentos/com.nsy.testefragmentos.TesteFragmentosActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.os.Looper.loop(Looper.java:123)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-24 11:46:16.708: E/AndroidRuntime(395):  at java.lang.reflect.Method.invokeNative(Native Method)
12-24 11:46:16.708: E/AndroidRuntime(395):  at java.lang.reflect.Method.invoke(Method.java:521)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-24 11:46:16.708: E/AndroidRuntime(395):  at dalvik.system.NativeStart.main(Native Method)
12-24 11:46:16.708: E/AndroidRuntime(395): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.Activity.setContentView(Activity.java:1647)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.nsy.testefragmentos.TesteFragmentosActivity.onCreate(TesteFragmentosActivity.java:12)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-24 11:46:16.708: E/AndroidRuntime(395):  ... 11 more
12-24 11:46:16.708: E/AndroidRuntime(395): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.nsy.testefragmentos-1.apk]
12-24 11:46:16.708: E/AndroidRuntime(395):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
12-24 11:46:16.708: E/AndroidRuntime(395):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
12-24 11:46:16.708: E/AndroidRuntime(395):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.createView(LayoutInflater.java:466)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
12-24 11:46:16.708: E/AndroidRuntime(395):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
12-24 11:46:16.708: E/AndroidRuntime(395):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
12-24 11:46:16.708: E/AndroidRuntime(395):  ... 20 more
12-24 11:46:21.828: I/Process(395): Sending signal. PID: 395 SIG: 9

I really don't know what I'm doing wrong =/ Can anyone help me? thnkx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回梦 2024-12-29 18:31:55

您的 Activity 应扩展 FragmentActivity 而不是 Activity。我自己也有同样的问题。

public class TesteFragmentosActivity extends FragmentActivity {
    ...
}

Your activity should extend FragmentActivity instead of Activity. Had the same problem myself.

public class TesteFragmentosActivity extends FragmentActivity {
    ...
}
放飞的风筝 2024-12-29 18:31:55

有时我遇到的错误(我的错误)是,我在一个文件中使用片段 (android.app.fragment) ,并在另一个文件中使用 Fragment(android.support.v4.app.Fragment) 调用它。在逐一检查行后,我发现了这个并解决了,对于这个错误我花了 5-6 个小时,我正在实现 ImageSlider

Sometimes the error (mistake of mine) I have faced that, I have using fragment (android.app.fragment) in one file and in another calling it with Fragment(android.support.v4.app.Fragment) . after checking line One'by one I found this and solved, for this mistake I have spent 5-6 hour, I was implementing ImageSlider

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文