我的 Android 应用程序的新版本由于格式错误 xml 而崩溃,inflater 无法膨胀 textview,但已定义 xmlns:android

New Version of my Android App crashes due to malformed xml, inflater fails to inflate textview, but xmlns:android is defined

我为设备 运行 4.1.1 开发了一个时钟应用程序,我的调试版本在测试时正在设备上运行。

我做了一些修改,添加了 1 个 textview 和一个包含 imageview 和 textview 的自定义 toast 视图。

当我在设备上安装应用程序时,它启动并崩溃并抛出此错误:

04-21 17:53:17.623 4542-4542/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zebra.ar05.ztimeclock/com.zebra.ar05.ztimeclock.MainActivity}: android.view.InflateException: Binary XML file line #24: Error inflating class TextView
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                                                 at android.app.ActivityThread.access0(ActivityThread.java:130)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                                                 at android.os.Handler.dispatchMessage(Handler.java:99)
                                                 at android.os.Looper.loop(Looper.java:137)
                                                 at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                 at java.lang.reflect.Method.invoke(Method.java:511)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                 at dalvik.system.NativeStart.main(Native Method)
                                              Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class TextView
                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
                                                 at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                 at com.zebra.ar05.ztimeclock.MainActivity.onCreate(MainActivity.java:44)
                                                 at android.app.Activity.performCreate(Activity.java:5081)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                                                 at android.app.ActivityThread.access0(ActivityThread.java:130) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                 at android.os.Looper.loop(Looper.java:137) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:4745) 
                                                 at java.lang.reflect.Method.invokeNative(Native Method) 
                                                 at java.lang.reflect.Method.invoke(Method.java:511) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                                                 at dalvik.system.NativeStart.main(Native Method) 
                                              Caused by: java.lang.NullPointerException
                                                 at android.support.v7.widget.ResourcesWrapper.getResourcePackageName(ResourcesWrapper.java:254)
                                                 at android.content.res.Resources.initializeRedirectionMap(Resources.java:2320)
                                                 at android.content.res.Resources.<init>(Resources.java:193)
                                                 at android.content.res.Resources.<init>(Resources.java:171)
                                                 at android.support.v7.widget.ResourcesWrapper.<init>(ResourcesWrapper.java:46)
                                                 at android.support.v7.widget.TintResources.<init>(TintResources.java:34)
                                                 at android.support.v7.widget.TintContextWrapper.<init>(TintContextWrapper.java:100)
                                                 at android.support.v7.widget.TintContextWrapper.wrap(TintContextWrapper.java:68)
                                                 at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:62)
                                                 at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:58)
                                                 at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
                                                 at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
                                                 at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
                                                 at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                                                 at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
                                                 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                 at com.zebra.ar05.ztimeclock.MainActivity.onCreate(MainActivity.java:44) 
                                                 at android.app.Activity.performCreate(Activity.java:5081) 
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                                                 at android.app.ActivityThread.access0(ActivityThread.java:130) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                 at android.os.Looper.loop(Looper.java:137) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:4745) 
                                                 at java.lang.reflect.Method.invokeNative(Native Method) 
                                                 at java.lang.reflect.Method.invoke(Method.java:511) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                                                 at dalvik.system.NativeStart.main(Native Method) 

好的,找到了实际的堆栈跟踪我的问题来自我的日期 textview 元素,这里是 xml:

    <TextView
    android:id="@+id/date_label"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:textSize="100sp"
    android:textColor="@color/textColor"
    android:text="DATE"/>

XML 验证器的输出是:

The prefix "android" for attribute "android:id" associated with an element type "TextView" is not bound.

现在真的很震惊...我该从这里去哪里?

我有最新的支持库:compile 'com.android.support:appcompat-v7:25.3.1'

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
tools:context="com.zebra.ar05.ztimeclock.MainActivity"
android:weightSum="1"
android:background="@color/colorBackground"
android:orientation="vertical">

<DigitalClock
    android:id="@+id/textClock"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:textColor="@color/textColor"
    android:textSize="150sp"
    android:textAlignment="center"
    android:gravity="center_horizontal"/>

<ProgressBar
    android:id="@+id/progress_bar"
    style="?android:attr/progressBarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="80sp"
    android:minHeight="100sp"
    android:minWidth="100sp"/>

</LinearLayout>

从主布局中删除 textview 后,应用程序运行正常。在自定义 toast 中加载 ImageView 或 Textview 时仍然崩溃...在模拟器 运行 相同 OS 版本中功能正常。会不会是4.1.1的自定义版本导致的?

该错误意味着您正尝试在 "time" 列中使用已在您的数据库中保存或使用的参数,因为它是您在数据库中设置的约束条件

我已经通过从我的主要活动 MainActivity extends AppCompatActivity 中删除 AppCompatActivity 并将其更改为 MainActivity extends Activity

解决了这个问题

并从我的 gradle.build 文件中删除 compile:android.appcompat.v7 引用。显然,我部署的设备不支持 AppCompat 库。是什么让我发现这实际上是在堆栈跟踪中,但隐藏得很深。

                                              Caused by: java.lang.NullPointerException
                                             at android.support.v7.widget.ResourcesWrapper.getResourcePackageName(ResourcesWrapper.java:254)
                                             at android.content.res.Resources.initializeRedirectionMap(Resources.java:2320)
                                             at android.content.res.Resources.<init>(Resources.java:193)
                                             at android.content.res.Resources.<init>(Resources.java:171)
                                             at android.support.v7.widget.ResourcesWrapper.<init>(ResourcesWrapper.java:46)
                                             at android.support.v7.widget.TintResources.<init>(TintResources.java:34)
                                             at android.support.v7.widget.TintContextWrapper.<init>(TintContextWrapper.java:100)
                                             at android.support.v7.widget.TintContextWrapper.wrap(TintContextWrapper.java:68)
                                             at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:62)
                                             at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:58)
                                             at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
                                             at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
                                             at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
                                             at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675)
                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                                             at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
                                             at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                             at com.zebra.ar05.ztimeclock.MainActivity.onCreate(MainActivity.java:44) 
                                             at android.app.Activity.performCreate(Activity.java:5081) 
                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                                             at android.app.ActivityThread.access0(ActivityThread.java:130) 
                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                                             at android.os.Handler.dispatchMessage(Handler.java:99) 
                                             at android.os.Looper.loop(Looper.java:137) 
                                             at android.app.ActivityThread.main(ActivityThread.java:4745) 
                                             at java.lang.reflect.Method.invokeNative(Native Method) 
                                             at java.lang.reflect.Method.invoke(Method.java:511) 
                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                                             at dalvik.system.NativeStart.main(Native Method)

A NullPointerExceptionandroid.support.v7.widget.AppCompatTextView 抛出,那时我意识到我实际上并没有在我的 activity_main.xml 布局文件中调用 TextView class,因为我使用的是 android.support.v7 库,所以我实际上是在调用设备不支持的 AppCompatTextView。至少它看起来是这样的。