Kotlin 编程语言中的 Android Studio 错误(RecyclerView 没有 LayoutManager)

Posted

技术标签:

【中文标题】Kotlin 编程语言中的 Android Studio 错误(RecyclerView 没有 LayoutManager)【英文标题】:Android Studio Error (RecyclerView has no LayoutManager) in Kotlin programing language 【发布时间】:2020-11-16 19:18:55 【问题描述】:

所以我在 android studio 上使用 Kotlin 编程语言创建 Recyclerview,当我运行我的代码时出现错误,我使用 Android Studio 版本 3.6.3 和 API 版本 30。

这是我的 MainActivity.kt :

class MainActivity : AppCompatActivity() 


override fun onCreate(savedInstanceState: Bundle?) 
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val exampleList = generateDummyList(500)

    recycler_view.adapter = ExampleAdapter(exampleList)
    recycler_view.layoutManager = LinearLayoutManager(this)
    recycler_view.setHasFixedSize(true)




private fun generateDummyList(size: Int): List<ExampleItem> 
    val list = ArrayList<ExampleItem>()

    for (i in 0 until size) 
        val drawable = when (i % 3) 
            0 -> R.drawable.abdulhalim
            1 -> R.drawable.abdulharisnasution
            else -> R.drawable.abdulmuis
        

        val item = ExampleItem(drawable, "Item $i", "Line 2")

        list += item
    
    return list

这是我的 ExampleAdapter.kt :

lass ExampleAdapter(private val exampleList: List<ExampleItem>) : RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder>() 


class ExampleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) 
    val imageView: ImageView = itemView.image_view
    val textView1: TextView = itemView.text_view1
    val textView2: TextView = itemView.text_view2


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder 
    val itemView = LayoutInflater.from(parent.context).inflate(
        R.layout.example_item,
        parent, false
    )

    return ExampleViewHolder(itemView)


override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) 
    val currentItem = exampleList[position]

    holder.imageView.setImageResource(currentItem.imageResource)
    holder.textView1.text = currentItem.text1
    holder.textView2.text = currentItem.text2


override fun getItemCount(): Int 
    TODO("Not yet implemented")

这是我的 ExampleItem.kt :

data class ExampleItem(val imageResource: Int, val text1: String, val text2: String) 

这是我的 main_activity.xml :

<RelativeLayout
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"
android:layout_
android:layout_
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_
    android:layout_
    android:clickable="true"
    android:clipToPadding="false"
    android:padding="4dp"
    android:scrollbars="vertical"
    tools:listitem="@layout/example_item" />

这是我的 example_item.xml :

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:layout_margin="4dp">


<RelativeLayout
    android:layout_
    android:layout_
    android:padding="8dp">

    <ImageView
        android:id="@+id/image_view"
        android:layout_
        android:layout_
        android:src="@drawable/abdulhalim" />

    <TextView
        android:id="@+id/text_view1"
        android:layout_
        android:layout_
        android:layout_toEndOf="@id/image_view"
        android:layout_toRightOf="@id/image_view"
        android:text="Line 1"
        android:textColor="#000000"
        android:textSize="18dp"
        android:textStyle="bold" />


    <TextView
        android:id="@+id/text_view2"
        android:layout_
        android:layout_
        android:layout_below="@id/text_view1"
        android:layout_toEndOf="@id/image_view"
        android:layout_toRightOf="@id/image_view"
        android:text="Line 2" />

</RelativeLayout>

这是我在 Android Studio 上的错误:

 java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.recyclerviewbeginners/com.example.recyclerviewbeginners.MainActivity: android.view.InflateException: Binary XML file line #9 in com.example.recyclerviewbeginners:layout/activity_main: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView233faa8 VFED..... ......I. 0,0-0,0, adapter:null, layout:null, context:com.example.recyclerviewbeginners.MainActivity@f0dfa51
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: android.view.InflateException: Binary XML file line #9 in com.example.recyclerviewbeginners:layout/activity_main: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView233faa8 VFED..... ......I. 0,0-0,0, adapter:null, layout:null, context:com.example.recyclerviewbeginners.MainActivity@f0dfa51
 Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView233faa8 VFED..... ......I. 0,0-0,0, adapter:null, layout:null, context:com.example.recyclerviewbeginners.MainActivity@f0dfa51
    at androidx.recyclerview.widget.RecyclerView.generateLayoutParams(RecyclerView.java:4514)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
    at com.example.recyclerviewbeginners.MainActivity.onCreate(MainActivity.kt:14)
    at android.app.Activity.performCreate(Activity.java:7995)
    at android.app.Activity.performCreate(Activity.java:7979)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

这是我的 AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.recyclerviewbeginners">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这是我的styles.xml:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

【问题讨论】:

【参考方案1】:

您能告诉我们 MainActivity.onCreate() 中的第 14 行是什么吗?

如果是这两行之一:

    val exampleList = generateDummyList(500)

    recycler_view.adapter = ExampleAdapter(exampleList)

尝试更改顺序,以便在调用 setAdapter() 之前设置布局管理器:

    recycler_view.layoutManager = LinearLayoutManager(this)
    recycler_view.setHasFixedSize(true)

    val exampleList = generateDummyList(500)

    recycler_view.adapter = ExampleAdapter(exampleList)

【讨论】:

【参考方案2】:

您必须在 RecyclerView xml 中添加 app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" 或删除 tools:listitem 属性

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_
    android:layout_
    android:clickable="true"
    android:clipToPadding="false"
    android:padding="4dp"
    android:scrollbars="vertical"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:listitem="@layout/example_item" />

【讨论】:

以上是关于Kotlin 编程语言中的 Android Studio 错误(RecyclerView 没有 LayoutManager)的主要内容,如果未能解决你的问题,请参考以下文章

Kotlin 中的 Android Dev,被教程弄糊涂了

以编程方式(Kotlin)在Android中修改和显示联系人的最佳方式?

Android 使用 Kotlin 重写 Gradle 文件

Android 使用 Kotlin 重写 Gradle 文件

Kotlin:崛起中的下一代编程语言

如何在 Android Studio (Kotlin) 中的流连接之前放置音频