我试图在主要活动中创建一个片段,所以稍后我可以动态添加元素,当我运行应用程序时,我收到此错误:
04-29 19:32:47.318 E/ AndroidRuntime(27813): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.myapp/com.mycompany.myapp.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class Fragment
当我尝试在main.xml中运行没有<Fragment>
标记的应用时,它可以正常工作
MainActivity:
package com.mycompany.myapp;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
main.xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Fragment
android:id="@+id/fL"
android:name="com.mycompany.myapp.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MyFragment:
package com.mycompany.myapp;
import android.os.Bundle;
import android.view.*;
import android.app.Fragment;
public class MyFragment extends Fragment
{
View root;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
root = inflater.inflate(R.layout.fraglayout, container, false);
return root;
}
}
fraglayout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.mycompany.myapp.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
也试过了
类=&#34; com.mycompany.myapp.MyFragment&#34;
而不是主要布局android:name
中的<fragment>
完整的logCat(E):
04-29 20:11:03.444 E / dalvikvm(4221):heapsize Dalvik_dalvik_system_VMRuntime_nativeMinimumHeapSize 0 04-29 20:11:03.444 E / dalvikvm(4221):heapsize Dalvik_dalvik_system_VMRuntime_nativeMinimumHeapSize 15154664 04-29 20:11:03.945 E / AndroidRuntime(4221):致命异常:主要 04-29 20:11:03.945 E / AndroidRuntime(4221):进程:com.mycompany.myapp,PID:4221 04-29 20:11:03.945 E / AndroidRuntime(4221):java.lang.RuntimeException:无法启动活动ComponentInfo {com.mycompany.myapp / com.mycompany.myapp.MainActivity}:android.view.InflateException:二进制XML文件行#6:错误膨胀类Fragment 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.app.ActivityThread.access $ 800(ActivityThread.java:151) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1266) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.os.Handler.dispatchMessage(Handler.java:102) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.os.Looper.loop(Looper.java:136) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.app.ActivityThread.main(ActivityThread.java:5421) 04-29 20:11:03.945 E / AndroidRuntime(4221):at java.lang.reflect.Method.invokeNative(Native Method) 04-29 20:11:03.945 E / AndroidRuntime(4221):at java.lang.reflect.Method.invoke(Method.java:515) 04-29 20:11:03.945 E / AndroidRuntime(4221):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:979) 04-29 20:11:03.945 E / AndroidRuntime(4221):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795) 04-29 20:11:03.945 E / AndroidRuntime(4221):at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 04-29 20:11:03.945 E / AndroidRuntime(4221):at dalvik.system.NativeStart.main(Native Method) 04-29 20:11:03.945 E / AndroidRuntime(4221):引起:android.view.InflateException:二进制XML文件行#6:错误膨胀类Fragment 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.view.LayoutInflater.inflate(LayoutInflater.java:492) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.view.LayoutInflater.inflate(LayoutInflater.java:397) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.view.LayoutInflater.inflate(LayoutInflater.java:353) 04-29 20:11:03.945 E / AndroidRuntime(4221):at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:296) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.app.Activity.setContentView(Activity.java:1941) 04-29 20:11:03.945 E / AndroidRuntime(4221):at com.mycompany.myapp.MainActivity.onCreate(MainActivity.java:13) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.app.Activity.performCreate(Activity.java:5264) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1099) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282) 04-29 20:11:03.945 E / AndroidRuntime(4221):... 12更多 04-29 20:11:03.945 E / AndroidRuntime(4221):引起:java.lang.ClassNotFoundException:没找到类&#34; android.view.Fragment&#34; on path:DexPathList [[zip file&#34; /data/app/com.mycompany.myapp-2.apk"],nativeLibraryDirectories = [/ data / app-lib / com.mycompany.myapp-2,/ vendor / lib,/ system / lib]] 04-29 20:11:03.945 E / AndroidRuntime(4221):at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) 04-29 20:11:03.945 E / AndroidRuntime(4221):at java.lang.ClassLoader.loadClass(ClassLoader.java:497) 04-29 20:11:03.945 E / AndroidRuntime(4221):at java.lang.ClassLoader.loadClass(ClassLoader.java:457) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.view.LayoutInflater.createView(LayoutInflater.java:559) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.view.LayoutInflater.onCreateView(LayoutInflater.java:652) 04-29 20:11:03.945 E / AndroidRuntime(4221):at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.view.LayoutInflater.onCreateView(LayoutInflater.java:669) 04-29 20:11:03.945 E / AndroidRuntime(4221):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694) 04-29 20:11:03.945 E / AndroidRuntime(4221):... 22更多
*抱歉无法合成它导致SE应用程序在尝试合成代码时崩溃。
答案 0 :(得分:2)
尝试使用小写&#34; f &#34;而不是大写字母&#34; F &#34;写main.xml
时Fragment
布局上的// <<<< start with lowercase
<fragment
android:id="@+id/fL"
android:name="com.mycompany.myapp.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
布局:
for (int i = 0; i < alItemDb.size(); i++) {
dbProvider.update(alItemDb.get(i));
}
关于我在另一个问题上找到的解决方案的一些解释:
在XML中定义的片段实际上并不是视图。全景 指定了name,或者它假定包的android.view和 找到其余的。但是片段只有在真正指定时才会被指定 包括android:name标签。它就像一个关键字,它是 通常用小写字母书写。因此,Fragment指的是班级, 和fragment是XML中用于片段的占位符。
正如安德鲁在评论中提到的,任何特殊项目,如 并且是小写的,因为它们不是 android.view&#39; S