我在运行这个简单的应用程序时遇到了困难。我正在尝试使用Fragments创建一个简单的模块化UI。但是没有编译时错误。我无法理解并纠正Logcat引发的错误。
05-04 20:28:04.984: D/jdwp(4039): sendBufferedRequest : len=0x53
05-04 20:28:04.986: W/asset(4039): AssetManager-->addDefaultAssets CIP path not exsit!
05-04 20:28:05.001: D/ServiceManager(4039): The name of the service is package
05-04 20:28:05.042: D/ServiceManager(4039): The name of the service is connectivity
05-04 20:28:05.108: D/ServiceManager(4039): The name of the service is window
05-04 20:28:05.109: D/ServiceManager(4039): The name of the service is accessibility
05-04 20:28:05.125: D/ServiceManager(4039): The name of the service is theme
05-04 20:28:05.127: W/asset(4039): AssetManager-->addDefaultAssets CIP path not exsit!
05-04 20:28:05.128: D/ThemeManager(4039): contextandroid.app.ContextImpl@423c57e0
05-04 20:28:05.128: D/ThemeManager(4039): create ThemeManager object
05-04 20:28:05.129: D/ThemeManager(4039): Res_clear()
05-04 20:28:05.139: W/asset(4039): AssetManager-->addDefaultAssets CIP path not exsit!
05-04 20:28:05.167: I/dalvikvm(4039): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
05-04 20:28:05.167: W/dalvikvm(4039): VFY: unable to resolve virtual method 11346: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
05-04 20:28:05.167: D/dalvikvm(4039): VFY: replacing opcode 0x6f at 0x0000
05-04 20:28:05.167: I/dalvikvm(4039): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
05-04 20:28:05.167: W/dalvikvm(4039): VFY: unable to resolve virtual method 11352: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
05-04 20:28:05.167: D/dalvikvm(4039): VFY: replacing opcode 0x6f at 0x0000
05-04 20:28:05.169: I/dalvikvm(4039): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
05-04 20:28:05.169: W/dalvikvm(4039): VFY: unable to resolve virtual method 9040: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
05-04 20:28:05.169: D/dalvikvm(4039): VFY: replacing opcode 0x6e at 0x000e
05-04 20:28:05.176: I/dalvikvm(4039): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
05-04 20:28:05.176: W/dalvikvm(4039): VFY: unable to resolve virtual method 368: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
05-04 20:28:05.176: D/dalvikvm(4039): VFY: replacing opcode 0x6e at 0x0002
05-04 20:28:05.176: I/dalvikvm(4039): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
05-04 20:28:05.176: W/dalvikvm(4039): VFY: unable to resolve virtual method 390: Landroid/content/res/TypedArray;.getType (I)I
05-04 20:28:05.176: D/dalvikvm(4039): VFY: replacing opcode 0x6e at 0x0002
05-04 20:28:05.216: D/ThemeManager(4039): Res_clear()
05-04 20:28:05.226: W/asset(4039): AssetManager-->addDefaultAssets CIP path not exsit!
05-04 20:28:05.243: W/ResourceType(4039): No package identifier when getting name for resource number 0x00000000
05-04 20:28:05.250: D/AndroidRuntime(4039): Shutting down VM
05-04 20:28:05.250: W/dalvikvm(4039): threadid=1: thread exiting with uncaught exception (group=0x41e519a8)
05-04 20:28:05.263: E/AndroidRuntime(4039): FATAL EXCEPTION: main
05-04 20:28:05.263: E/AndroidRuntime(4039): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nikhil.fragmentmodularui/com.nikhil.fragmentmodularui.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2350)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2402)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.app.ActivityThread.access$600(ActivityThread.java:162)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.os.Handler.dispatchMessage(Handler.java:107)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.os.Looper.loop(Looper.java:194)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.app.ActivityThread.main(ActivityThread.java:5410)
05-04 20:28:05.263: E/AndroidRuntime(4039): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 20:28:05.263: E/AndroidRuntime(4039): at java.lang.reflect.Method.invoke(Method.java:525)
05-04 20:28:05.263: E/AndroidRuntime(4039): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-04 20:28:05.263: E/AndroidRuntime(4039): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-04 20:28:05.263: E/AndroidRuntime(4039): at dalvik.system.NativeStart.main(Native Method)
05-04 20:28:05.263: E/AndroidRuntime(4039): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
05-04 20:28:05.263: E/AndroidRuntime(4039): at com.nikhil.fragmentmodularui.MainActivity.onCreate(MainActivity.java:14)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.app.Activity.performCreate(Activity.java:5122)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
05-04 20:28:05.263: E/AndroidRuntime(4039): ... 11 more
05-04 20:28:05.263: E/AndroidRuntime(4039): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.nikhil.interfragsavestate.FragmentA: make sure class name exists, is public, and has an empty constructor that is public
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.app.Fragment.instantiate(Fragment.java:592)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.app.Fragment.instantiate(Fragment.java:560)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.app.Activity.onCreateView(Activity.java:4727)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
05-04 20:28:05.263: E/AndroidRuntime(4039): ... 21 more
05-04 20:28:05.263: E/AndroidRuntime(4039): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.nikhil.interfragsavestate.FragmentA" on path: DexPathList[[zip file "/data/app/com.nikhil.fragmentmodularui-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.nikhil.fragmentmodularui-1, /vendor/lib, /system/lib]]
05-04 20:28:05.263: E/AndroidRuntime(4039): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
05-04 20:28:05.263: E/AndroidRuntime(4039): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-04 20:28:05.263: E/AndroidRuntime(4039): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-04 20:28:05.263: E/AndroidRuntime(4039): at android.app.Fragment.instantiate(Fragment.java:582)
05-04 20:28:05.263: E/AndroidRuntime(4039): ... 24 more
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00BBFF"
android:id="@+id/my_layout" >
<fragment
android:id="@+id/fragment1"
android:name="com.nikhil.interfragsavestate.FragmentA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<fragment
android:id="@+id/fragment2"
android:name="com.nikhil.interfragsavestate.FragmentB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/fragment1" />
</RelativeLayout>
答案 0 :(得分:0)
请更正您的xml文件activity_main.xml。获取Fragment的路径是错误的,我们必须指定FragmentA和FragmentB的完整路径
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00BBFF"
android:id="@+id/my_layout" >
<fragment
android:id="@+id/fragment1"
android:name="com.nikhil.fragmentmodularui.FragmentA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<fragment
android:id="@+id/fragment2"
android:name="com.nikhil.fragmentmodularui.FragmentB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/fragment1" />
</RelativeLayout>