Android Fragments 扩展布局时出错-扩展类片段时出错

ioekq8ef  于 12个月前  发布在  Android
关注(0)|答案(1)|浏览(138)

我试图在main activity中创建一个片段,以便稍后可以动态添加元素,当我运行应用程序时,我得到这个错误:

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>标签运行应用程序时,它可以正常工作。
主要活动:

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>


还尝试在主布局<fragment>中使用class=“com.mycompany.myapp.MyFragment”而不是android:name
全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): FATAL EXCEPTION: main
04-29 20:11:03.945 E/AndroidRuntime(4221): Process: com.mycompany.myapp, PID: 4221
04-29 20:11:03.945 E/AndroidRuntime(4221): 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
04-29 20:11:03.945 E/AndroidRuntime(4221):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
04-29 20:11:03.945 E/AndroidRuntime(4221):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
04-29 20:11:03.945 E/AndroidRuntime(4221):  at android.app.ActivityThread.access$800(ActivityThread.java:151)
04-29 20:11:03.945 E/AndroidRuntime(4221):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1266)
04-29 20:11:03.945 E/AndroidRuntime(4221):  at android.os.Handler.dispatchMessage(Handler.java:102)
04-29 20:11:03.945 E/AndroidRuntime(4221):  at android.os.Looper.loop(Looper.java:136)
04-29 20:11:03.945 E/AndroidRuntime(4221):  at 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): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class Fragment
04-29 20:11:03.945 E/AndroidRuntime(4221):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
04-29 20:11:03.945 E/AndroidRuntime(4221):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-29 20:11:03.945 E/AndroidRuntime(4221):  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-29 20:11:03.945 E/AndroidRuntime(4221):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-29 20:11:03.945 E/AndroidRuntime(4221):  at 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):  at 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):  at android.app.Activity.performCreate(Activity.java:5264)
04-29 20:11:03.945 E/AndroidRuntime(4221):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1099)
04-29 20:11:03.945 E/AndroidRuntime(4221):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
04-29 20:11:03.945 E/AndroidRuntime(4221):  ... 12 more
04-29 20:11:03.945 E/AndroidRuntime(4221): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.Fragment" on path: DexPathList[[zip file "/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):  at android.view.LayoutInflater.createView(LayoutInflater.java:559)
04-29 20:11:03.945 E/AndroidRuntime(4221):  at 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):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
04-29 20:11:03.945 E/AndroidRuntime(4221):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
04-29 20:11:03.945 E/AndroidRuntime(4221):  ... 22 more

dluptydi

dluptydi1#

尝试在Fragment布局中使用小写字母"f"而不是小写字母"F",同时编写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" />

字符串
关于我在另一个问题上找到的解决方案的一些解释:
在XML中定义的Fragment实际上不是视图。完整的视图名称被指定,或者它假定包的android. view并找到其余部分。但是只有当包含android:name标记时,fragment才真正被指定。它的作用类似于关键字,通常以小写形式编写。因此,Fragment引用类,fragment是XML中fragment的占位符。
正如安德鲁在评论中提到的,任何特殊项目,如和都是小写的,因为它们不是android. view的。
来源:https://stackoverflow.com/a/21948080/3669559

相关问题