【问题标题】:Android code crashes when opening in an emulatorAndroid 代码在模拟器中打开时崩溃
【发布时间】:2019-07-25 05:07:40
【问题描述】:

我对 java 很陌生,但出于某种原因,试图创建一个应用程序 该应用程序在 android studio 中正确构建,但是,当尝试在模拟器中打开时,它崩溃了。 This 是我的完整代码

是应用程序的zip,我猜,主要错误是

final View rootView = inflater.inflate(R.layout.fragment_sun, container, false); 

Logcat:错误

2019-07-25 10:42:48.002 5383-5383/com.example.phocast E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.phocast, PID: 5383
    android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.v7.widget.RecyclerView
    Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.RecyclerView
    Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.RecyclerView" on path: DexPathList[[zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/base.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_resources_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/lib/x86, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.view.LayoutInflater.createView(LayoutInflater.java:606)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at com.example.phocast.ui.main.SunFragment.onCreateView(SunFragment.java:69)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)

SunFragment.java 代码的第 69 行。

我不明白 XML 出了什么问题。

对应的XML是:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android" />

有人可以检查一下吗?

build.gradel

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.1"
    defaultConfig {
        applicationId "com.example.phocast"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.android.material:material:1.0.0'
    implementation'com.google.android.gms:play-services-places:17.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'org.shredzone.commons:commons-suncalc:2.6'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.google.android.gms:play-services-location:17.0.0'
}

【问题讨论】:

标签: java android xml layout


【解决方案1】:

使用

androidx.recyclerview.widget.RecyclerView

fragment_sun.xml 中而不是

android.support.v7.widget.RecyclerView

因为在 build.gradle 中依赖是 androidx.recyclerview:recyclerview:1.0.0 但在 SunFragment 中 RecyclerView 是从 androidx 导入的

【讨论】:

  • 我已经尝试过了,但没有任何改进,因为 android studio 建议使用 androidx。一件事是,当我尝试折射时,这会显示“未找到使用情况”。
  • 另外,当我尝试将 card_item.xml 更改为 androidx 时,我收到 errorerror: not well-formed (invalid token). /home/rudra/Projects/Phocast/app/src/main/res/layout/card_item.xml: error: file failed to compile.
  • @BaRud :当我们使用“迁移到 androidx”时,xml 文件中出现问题。我遇到了同样的问题。您需要手动为 xml 文件执行此操作
  • @BaRud:使用androidx.cardview.widget.CardView 进行卡片查看。当你输入像&lt;Card 这样的视图名称时,编辑器会建议你同时导入androidx 和support.v7。并确保您更改了打开和关闭标签,例如&lt;androidx.cardview.widget.CardView&gt;... &lt;/androidx.cardview.widget.CardView&gt;
  • 尝试使缓存失效,重启,重建项目,然后使用androidx。
【解决方案2】:

您正在使用androidx 支持库,因此请使用androidx.recyclerview.widget.RecyclerView

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" />

【讨论】:

    【解决方案3】:

    以下截图来自您的项目搜索结果:

    尝试将所有android.support.v7 包替换为对应的androidx

    请参阅Migrating to AndroidX 了解详细的包映射。

    例如改变

    android.support.v7.widget.RecyclerView
    

    androidx.recyclerview.widget.RecyclerView
    

    这是因为您的源代码使用 AndroidsX 包,但您的 xml 布局声明使用 android support 包这是错误的。

    【讨论】:

      猜你喜欢
      • 2019-07-06
      • 2020-08-02
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      相关资源
      最近更新 更多