从布局android-support-v4扩展片段时出错

时间:2011-12-24 14:10:48

标签: android android-layout android-fragments android-inflate android-support-library

我在使用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

2 个答案:

答案 0 :(得分:6)

您的活动应该延长FragmentActivity而不是Activity。我自己也有同样的问题。

public class TesteFragmentosActivity extends FragmentActivity {
    ...
}

答案 1 :(得分:0)

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