3

I am making a relatively simple app for a school project. This is my first time working with android studio btw. The purpose of the app is to be able to draw inside of a given area, the code for the drawing part I copied from here: https://guides.codepath.com/android/Basic-Painting-with-Views I'm pretty sure that android studio converted the code into kotlin when I pasted it in, but I might be wrong about that.

The error is showing up in my androidmanifest, in the line '<activity android:name"com.example.colourkiller.Drawactivity". The "com.example.colourkiller.Drawactivity" is marked in red, and the error says: "Drawactivity must extend android.app.activity" and "This class should provide a default instructor"

AndroidManifest:

<?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>

MainActivity:

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 (layout):

<?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>

I've already tried:

  1. Updating everything to the latest versions
  2. Rebuilding the project
  3. Invalidate Cashes/restart

I think the problem is in the defining of the class in MainActivity, but as this is my first time using androidstudio I'm not too sure. Any help is greatly appreciated!

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Kaasper
  • 33
  • 1
  • 3
  • 4
    As the error says `DrawActivity` is not an activity you have it extending a `View` – tyczj Mar 24 '21 at 14:36
  • Ah right, I see. So how should I formulate that then? Excuse my inexperience, and thanks for your help! – Kaasper Mar 24 '21 at 15:02

1 Answers1

1

Create MainActivity class which extends AppCompatActivity:

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

And Manifest.xml, modify DrawActivity to MainActivity:

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

Above is an outline. There can be more modifications to solve your problem.

For your DrawActivity is actually a View as @tyczj suggested, you should rename it like DrawView.

hata
  • 11,633
  • 6
  • 46
  • 69
  • Thank you so much! I spent a few hours too much trying to solve this on my own... I ran it and everything works now, great! – Kaasper Mar 24 '21 at 15:15