难以找到 XML 和 Kotlin 中“无法解析类”错误的解决方案

问题描述 投票:0回答:1

我对 Android 应用程序开发有点陌生,我正在尝试对这个应用程序进行编程,它基本上取代了纸牌游戏中的计数系统。问题是,当我尝试使用 XML 创建布局时,收到错误“无法解析类”。此错误不是编译器窗口中的错误消息,但如果将鼠标悬停在某个标签的文本上,您会看到它。如果我尝试访问此视图,应用程序就会崩溃。我的设备上没有收到任何错误消息或任何东西。谁能帮我? 我的 XML 代码现在看起来像这样:

// This AddUser-Tag in the following line is the point where i see the error message
<AddUser xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/buttonPanel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I'm a button" />



</AddUser>

我的 Kotlin 代码如下:

package com.example.schaetzen

import android.app.Activity
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.schaetzen.ui.theme.SchaetzenTheme

class ATTYN {

    companion object {
        // Erstellen der Objektklasse für Spieler + Punkte
        data class Player(val name:String, var Points: List<Int>)

        // Erstellen der Variable, die die Liste aller Spieler darstellt
        var AllPlayers = arrayOf<Player>()
    }

}




class MainActivity : ComponentActivity() {
    val create = onCreate()


    fun onCreate() {

        setContentView(R.layout.adduser)

    }

}

class AddUser{

}

我尝试在 MainActivity.kt 文件中创建一个类,但这没有帮助,并且错误仍然存在。我也尝试在谷歌上搜索,但我只找到了与我不同的案例。

android xml kotlin class user-interface
1个回答
0
投票

我假设 xml 是一个布局?您需要 AddUser 是一个合法的 View(它需要从 View 派生,但在您的代码中并不存在),并且您需要使用完全限定的类名 com.example.schaetzen.AddUser。如果你想在其中包含其他视图,它需要是一个 ViewGroup,而不仅仅是一个 View。

尽管您很可能不应该执行任何此操作 - 为什么要将顶级视图设为自定义视图类型?这样做是有理由的,但这是非常不寻常的。您很可能只是误解了如何编写 UI 并做了您实际上不想做的事情。

© www.soinside.com 2019 - 2024. All rights reserved.