从布局 android-support-v4 膨胀片段时出错
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 Activity 应扩展
FragmentActivity
而不是Activity
。我自己也有同样的问题。Your activity should extend
FragmentActivity
instead ofActivity
. Had the same problem myself.有时我遇到的错误(我的错误)是,我在一个文件中使用片段 (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