首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >误差膨胀android.support.v7.widget.CardView

误差膨胀android.support.v7.widget.CardView
EN

Stack Overflow用户
提问于 2020-04-22 15:18:40
回答 2查看 450关注 0票数 0

在深入研究代码之前,我确实想提到Android说无法找到以下类: android.support.v7.widget.CardView,,尽管我已经下载了它。

(错误地说没有找到CardView,但没有找到下载选项。)

下面是发生错误的活动的代码。

ContactsActivity.kt

(contactView是RecyclerView)

代码语言:javascript
运行
复制
package com.smartherd.msgshareapp

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.smartherd.msgshareapp.models.Contact
import com.smartherd.msgshareapp.models.adapters.ContactAdapter
import kotlinx.android.synthetic.main.activity_contacts.*

class ContactsActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.VERTICAL

        contactsView.layoutManager = layoutManager  // Oops! contactsView is NULL!

        contactsView.adapter = ContactAdapter(this, Contact.allContacts)
    }
}

这是activity_contact.xml

代码语言:javascript
运行
复制
<?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"
    android:baselineAligned="false"
    android:orientation="vertical"
    android:padding="10dp">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/contactsView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />
</LinearLayout>

接触类

代码语言:javascript
运行
复制
package com.smartherd.msgshareapp.models

import org.json.JSONArray
import org.json.JSONObject
import java.io.File

data class Contact(val name: String, val phone: String, val email: String) {
    companion object {

       val allContacts: List<Contact>
        get() {
            val json = JSONObject(File("contacts.json").readText()).getJSONArray("contacts")
            // Looks like {"contacts": [{"name": ..., "email": ..., "phone": ...}, ...]}

            val contacts = mutableListOf<Contact>()

            var i = 0

            while (i < json.length()) {
                val name = json.getJSONObject(i).getString("name")
                val phone = json.getJSONObject(i).getString("phone")
                val email = json.getJSONObject(i).getString("email")

                contacts.add(Contact(name, phone, email))

                ++i
            }

            return contacts
        }

    }
}

和Contact Adapter类(ContactAdapter.kt)

代码语言:javascript
运行
复制
package com.smartherd.msgshareapp.models.adapters

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.smartherd.msgshareapp.R
import com.smartherd.msgshareapp.models.Contact
import kotlinx.android.synthetic.main.card_contact.view.*

class ContactAdapter(val context: Context, val contacts: List<Contact>) :
    RecyclerView.Adapter<ContactAdapter.ContactHolder>() {

    inner class ContactHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun initialize(contact: Contact) {
            itemView.contactName.text = "${contact.name}\n${contact.email}"
            // itemView.contactImage is not set yet.
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContactHolder =
        ContactHolder(
            LayoutInflater.from(context).inflate(R.layout.card_contact, parent, false)
        )

    override fun getItemCount(): Int = contacts.size

    override fun onBindViewHolder(holder: ContactHolder, position: Int) = holder.initialize(contacts[position])
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-22 15:22:47

首先添加setContentView();

代码语言:javascript
运行
复制
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main) //missing
    val recyclerView = findViewById(R.id.contactsView) as RecyclerView

使用精确版本App Level build.gradle

代码语言:javascript
运行
复制
 dependencies {

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

仅供参考

你使用的是非常旧的版本。如果您想使用最新版本,那么除非您在应用程序中进行以下更改,否则库将无法工作:

later.

  • Update

  • 将compileSdkVersion升级到28或使用Jetpack (AndroidX).

AndroidX将原来的支持库API替换为androidx命名空间中的包。阅读关于的官方指南。

您的Cardview & recyclerview

代码语言:javascript
运行
复制
 implementation 'androidx.cardview:cardview:1.0.0'
 implementation 'androidx.recyclerview:recyclerview:1.1.0'
票数 2
EN

Stack Overflow用户

发布于 2020-04-23 06:51:18

好吧,在@IntellijAmiya的帮助下,我想这基本上是因为我在app/build.gradle中使用了"android.support.v7.widget.CardView“,而我应该使用androidX。此外,在设计卡的XML文件中,我没有使用androidX替代。把它修好了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61368711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档