Android-启动时异常(对于某些用户)

时间:2019-11-07 22:59:29

标签: android exception startup androidx

我最近从v4支持/兼容库移到了androidx,尽管它似乎运行良好(至少在我运行过的任何设备/模拟器上),但某些用户似乎遇到了崩溃。我不能保证这是由于androidx迁移引起的,但是这是个可疑的时机。

这是崩溃的堆栈跟踪记录(FileActivity.java是我的启动活动):

java.lang.RuntimeException:
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2473)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2541)
  at android.app.ActivityThread.access$800 (ActivityThread.java:160)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1321)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:135)
  at android.app.ActivityThread.main (ActivityThread.java:5597)
  at java.lang.reflect.Method.invoke (Method.java)
  at java.lang.reflect.Method.invoke (Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:984)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:779)

Caused by: android.view.InflateException: 
  at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:761)
  at android.view.LayoutInflater.rInflate (LayoutInflater.java:810)
  at android.view.LayoutInflater.rInflate (LayoutInflater.java:813)
  at android.view.LayoutInflater.inflate (LayoutInflater.java:508)
  at android.view.LayoutInflater.inflate (LayoutInflater.java:415)
  at android.view.LayoutInflater.inflate (LayoutInflater.java:365)
  at androidx.appcompat.app.AppCompatDelegateImpl.setContentView (AppCompatDelegateImpl.java:555)
  at androidx.appcompat.app.AppCompatActivity.setContentView (AppCompatActivity.java:161)
  at com.me.myapp.FileActivity.onCreate (FileActivity.java:104)
  at android.app.Activity.performCreate (Activity.java:6010)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1122)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2426)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2541)
  at android.app.ActivityThread.access$800 (ActivityThread.java:160)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1321)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:135)
  at android.app.ActivityThread.main (ActivityThread.java:5597)
  at java.lang.reflect.Method.invoke (Method.java)
  at java.lang.reflect.Method.invoke (Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:984)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:779)

Caused by: java.lang.ClassNotFoundException:
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    at android.view.LayoutInflater.createView(LayoutInflater.java:575)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747)
    ... 21 more

和犯罪现场:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_file);
    ...

据我所知,在夸大activity_file布局时,它找不到一个类:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"/>

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar">

            <ListView
                android:id="@+id/listView"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_below="@+id/toolbar"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:choiceMode="singleChoice"
                android:clickable="true"
                android:clipToPadding="false"
                android:divider="@color/divider"
                android:dividerHeight="1dp"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:headerDividersEnabled="true"
                android:longClickable="true"
                android:minHeight="64dp"
                android:paddingBottom="72sp"
                android:scrollingCache="false"
                android:stackFromBottom="false"/>

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

        <TextView
            android:id="@+id/no_documents"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="false"
            android:focusable="false"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/no_documents_found"
            android:textSize="24sp"
            android:textStyle="bold"
            android:visibility="visible"/>

        <ProgressBar
            android:id="@+id/progressBar"
            style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Large"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerInParent="true"
            android:foregroundGravity="center_vertical|center_horizontal"
            android:visibility="invisible"/>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/floatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            android:clickable="true"
            android:focusable="true"
            android:maxWidth="24dp"
            android:maxHeight="24dp"
            app:backgroundTint="@color/colorPrimary"
            app:fabSize="normal"
            app:srcCompat="@drawable/fab_plus" />

    </RelativeLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/file_menu"/>

</androidx.drawerlayout.widget.DrawerLayout>

在很大程度上,引用的toolbardrawer_header布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:elevation="4dp">

</androidx.appcompat.widget.Toolbar>


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="96dp"
                android:background="@drawable/drawer_background"
                android:orientation="vertical"
                android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/documents"
        android:scaleType="centerCrop"
        android:visibility="gone"
        app:srcCompat="@drawable/drawer_wallpaper"/>

    <ImageView
        android:id="@+id/avatar"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_margin="@dimen/spacing_large"
        android:elevation="4dp"
        android:src="@drawable/ic_launcher"/>

</RelativeLayout>

然后(根据要求)file_menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools" tools:context=".FileActivity">
    <group android:id="@+id/documents">
        <item
            android:id="@+id/action_documents"
            android:checkable="true"
            android:checked="true"
            android:icon="@drawable/blank"
            android:title="@string/documents"/>
    </group>
    <group android:id="@+id/source">
        <item
            android:id="@+id/action_googledrive"
            android:icon="@drawable/googledrive"
            android:title="@string/googledrive"/>
        <item
            android:id="@+id/action_dropbox"
            android:icon="@drawable/dropbox"
            android:title="@string/dropbox"/>
        <item
            android:id="@+id/action_onedrive"
            android:icon="@drawable/onedrive"
            android:title="@string/onedrive"/>
        <item
            android:id="@+id/action_device"
            android:icon="@drawable/device"
            android:title="@string/device_storage"/>
    </group>
    <group android:id="@+id/settings">
        <item
            android:id="@+id/action_settings"
            android:icon="@drawable/gear"
            android:title="@string/action_settings"/>
        <item
            android:id="@+id/action_upgrade"
            android:icon="@drawable/upgrade"
            android:title="@string/action_upgrade_now"/>
    </group>
    <group android:id="@+id/recover">
        <item
            android:id="@+id/action_recover"
            android:icon="@drawable/blank"
            android:title="@string/recover"/>
    </group>
    <item
        android:id="@+id/action_help"
        android:icon="@drawable/help"
        android:title="@string/action_help"/>
    <item
        android:id="@+id/action_about"
        android:icon="@drawable/blank"
        android:title="@string/action_about"/>
</menu>

这里是否缺少某些仅在某些设备上会导致崩溃的东西?可能是早期版本的Android?

(这可能是比较棘手的事实,因为其他一些用户似乎在启动时崩溃,报告仅包含{em> java.lang.RuntimeException,而堆栈跟踪没有“进一步”。我不知道这是否相关,但首先要注意)。

1 个答案:

答案 0 :(得分:0)

我复制并粘贴了您的工作,在我的计算机上工作正常。因此,可能是另一个布局文件引起了问题。当您迁移到AndroidX时,是否让Android Studio帮您做到了?在Android Studio中,请遵循Refactor > Migrate to AndroidX为您执行此操作。 但是,您必须使用任何较旧的依赖项来手动迁移xml布局文件。因此,检查您的Gradle文件,并针对看到的每个androidx依赖项,检查您的xml文件是否未使用“支持”版本。例如,您不应看到任何“ android.support”。在您的任何xml布局文件中。