开始活动时,膨胀类android.support.v7.widget.RecyclerView时出错

时间:2019-06-02 17:38:46

标签: android kotlin android-recyclerview android-support-library

我是第一次尝试在Android中使用recyleview。 我遵循了本教程 https://developer.android.com/guide/topics/ui/layout/recyclerview

该应用是新鲜的,仅包含这一活动。现在的目标只是使用recyleview显示字符串列表。 该应用程序已编译,但是如果我启动它(在虚拟设备api 26上),则会因错误而崩溃。

我曾尝试使用API​​ lvl 26和28来启动。我还尝试过更改支持库的版本,但这完全破坏了我的第一个应用程序...

我已经阅读了很多有关该问题的Google搜索结果。但是它们似乎都有不同的非常具体的原因。我无法解决我的问题。

我的家属:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
}

我的活动:

package com.example.myfirstapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {
    private lateinit var recyclerView: RecyclerView
    private lateinit var viewAdapter: RecyclerView.Adapter<*>
    private lateinit var viewManager: RecyclerView.LayoutManager
    private val myArray = arrayOf("Hans","Klaus")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewManager = LinearLayoutManager(this)
        viewAdapter = MyAdapter(myArray)
        recyclerView = findViewById<RecyclerView>(R.id.my_recycler_view).apply {
            setHasFixedSize(true)
            layoutManager = viewManager
            adapter = viewAdapter
        }
    }
}
class MyAdapter(private val myDataset: Array<String>) :
    RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
    class MyViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyAdapter.MyViewHolder {
        val textView = LayoutInflater.from(parent.context)
            .inflate(R.layout.my_text_view, parent, false) as TextView
        return MyViewHolder(textView)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.textView.text = myDataset[position]
    }
    override fun getItemCount() = myDataset.size
}

部分错误引起的:

Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class android.support.v7.widget.RecyclerView
     Caused by: android.view.InflateException: Binary XML file line #9: 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.myfirstapp-O0jrBUQE1V6V1rEdCmyGrg==/base.apk", zip file "/data/app/com.example.myfirstapp-O0jrBUQE1V6V1rEdCmyGrg==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.myfirstapp-O0jrBUQE1V6V1rEdCmyGrg==/split_lib_resources_apk.apk", zip file "/data/app/com.example.myfirstapp-O0jrBUQE1V6V1rEdCmyGrg==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.myfirstapp-O0jrBUQE1V6V1rEdCmyGrg==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.myfirstapp-O0jrBUQE1V6V1rEdCmyGrg==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.myfirstapp-O0jrBUQE1V6V1rEdCmyGrg==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.myfirstapp-O0jrBUQE1V6V1rEdCmyGrg==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.myfirstapp-O0jrBUQE1V6V1rEdCmyGrg==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.myfirstapp-O0jrBUQE1V6V1rEdCmyGrg==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.myfirstapp-O0jrBUQE1V6V1rEdCmyGrg==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.myfirstapp-O0jrBUQE1V6V1rEdCmyGrg==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.myfirstapp-O0jrBUQE1V6V1rEdCmyGrg==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.myfirstapp-O0jrBUQE1V6V1rEdCmyGrg==/lib/x86, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)

2 个答案:

答案 0 :(得分:2)

RecyclerView现在是AndroidX软件包的一部分。 尝试改用androidx.recyclerview.widget.RecyclerView

只需使用此依赖项

 implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha05'

答案 1 :(得分:2)

问题可能是由于您使用的是androidx(请参见导入)而引起的,但是在gradle文件中包含了android.support,因此要解决此问题,您应该进行以下更改:

implementation 'com.android.support:recyclerview-v7:28.0.0'

androidx.recyclerview:recyclerview:1.0.0

还要确保对您的xml文件进行更改,而不是:

<android.support.v7.widget.RecyclerView>

使用

<androidx.recyclerview.widget.RecyclerView>