gpt4 book ai didi

java - Android Studio 错误 : Activity must extend android. app.activity

转载 作者:行者123 更新时间:2023-12-05 06:02:57 39 4
gpt4 key购买 nike

我正在为学校项目制作一个相对简单的应用程序。这是我第一次使用 android studio btw。该应用程序的目的是能够在给定区域内绘制,我从此处复制的绘图部分代码:https://guides.codepath.com/android/Basic-Painting-with-Views我很确定当我粘贴代码时,android studio 将代码转换为 kotlin,但我可能错了。

错误出现在我的 androidmanifest 中,在行 '

安卓 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.colourkiller">

<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/Theme.ColourKiller">
<activity android:name="com.example.colourkiller.DrawActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

主要 Activity :

package com.example.colourkiller

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View


public class DrawActivity(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
// setup initial color
private val paintColor = Color.BLACK

// defines paint and canvas
private var drawPaint: Paint? = null

// stores next circle
private val path = Path()
private fun setupPaint() {
// Setup paint with color and stroke styles
drawPaint = Paint()
drawPaint!!.color = paintColor
drawPaint!!.isAntiAlias = true
drawPaint!!.strokeWidth = 5f
drawPaint!!.style = Paint.Style.STROKE
drawPaint!!.strokeJoin = Paint.Join.ROUND
drawPaint!!.strokeCap = Paint.Cap.ROUND
}

override fun onDraw(canvas: Canvas) {
canvas.drawPath(path, drawPaint!!)
}

override fun onTouchEvent(event: MotionEvent): Boolean {
val pointX = event.x
val pointY = event.y
when (event.action) {
MotionEvent.ACTION_DOWN -> {
path.moveTo(pointX, pointY)
return true
}
MotionEvent.ACTION_MOVE -> path.lineTo(pointX, pointY)
else -> return false
}
// Force a view to draw again
postInvalidate()
return true
}

init {
isFocusable = true
isFocusableInTouchMode = true
setupPaint()
}
}

Activity_main.xml(布局):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".DrawActivity" >


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<com.example.colourkiller.DrawActivity
android:id="@+id/simpleDrawingView1"
android:layout_width="385dp"
android:layout_height="583dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="13dp"
android:layout_marginTop="69dp"
android:layout_marginEnd="13dp"
android:layout_marginBottom="79dp" />

</RelativeLayout>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#D3CACA"
android:text="@string/app_name"
android:textAllCaps="true"
android:textColor="#2196F3"
android:textColorHighlight="#930F0F"
android:textSize="30sp"
android:textStyle="bold"
android:translationZ="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.018" />

<View
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#A6A3A3"
app:layout_constraintBottom_toBottomOf="@+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />

<Button
android:id="@+id/Saved"
android:layout_width="111dp"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="12dp"
android:backgroundTint="#2196F3"
android:text="@string/saved"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/Colours"
android:layout_width="111dp"
android:layout_height="60dp"
android:layout_marginBottom="12dp"
android:backgroundTint="#2196F3"
android:text="@string/Colours"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.946"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/KILL"
android:layout_width="111dp"
android:layout_height="60dp"
android:layout_marginStart="32dp"
android:layout_marginEnd="16dp"
android:backgroundTint="@color/red"
android:text="@string/KILL"
android:textSize="24sp"
android:textStyle="bold"
app:iconTint="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.476"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.98" />

<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#A6A3A3"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="0dp" />

<View
android:layout_width="12dp"
android:layout_height="match_parent"
android:background="#A6A3A3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<View
android:layout_width="12dp"
android:layout_height="match_parent"
android:background="#A6A3A3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

我已经试过了:

  1. 将所有内容更新到最新版本
  2. 重建项目
  3. 使现金无效/重新开始

我认为问题在于 MainActivity 中类的定义,但由于这是我第一次使用 androidstudio,所以我不太确定。非常感谢任何帮助!

最佳答案

创建扩展 AppCompatActivityMainActivity 类:

public class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

Manifest.xml,修改DrawActivityMainActivity:

<activity android:name="com.example.colourkiller.MainActivity"
android:label="@string/app_name">

以上是大纲。可以有更多的修改来解决你的问题。

因为您的 DrawActivity 实际上是 @tyczj 建议的 View ,您应该将其重命名为 DrawView。

关于java - Android Studio 错误 : Activity must extend android. app.activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66783279/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com