我是 Kotlin 的新手,我正在尝试创建一个函数,它会对滑动操作使用react。但是我有麻烦。
当我删除 abstract 关键字时,它会抛出一个错误:
Class 'dock' is not abstract and does not implement abstract member
当我保留 abstract 关键字时,应用程序不想以错误开头:
java.lang.Class<kiwi.you.app.dock> cannot be instantiated
码头.kt
import android.os.Bundle
import android.content.Intent
import android.view.GestureDetector
import android.view.MotionEvent
import androidx.appcompat.app.AppCompatActivity
import android.widget.Toast
/* CODE */
abstract class dock : AppCompatActivity(), GestureDetector.OnGestureListener
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.dock)
hide_bars()
}
/* slot swipe */
private fun slotSwipe0A()
{
val intent = Intent(this, zeroA::class.java)
startActivity(intent)
overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out)
}
private fun slotSwipe0B()
{
val intent = Intent(this, zeroB::class.java)
startActivity(intent)
overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out)
}
private fun slotSwipe1()
{
val intent = Intent(this, one::class.java)
startActivity(intent)
overridePendingTransition(R.anim.slide_up_in, R.anim.slide_down_out)
}
private fun slotSwipeMinus1()
{
val intent = Intent(this, minus1::class.java)
startActivity(intent)
overridePendingTransition(R.anim.slide_down_in, R.anim.slide_up_out)
}
private val swipeThreshold = 100
private val swipeVelocityThreshold = 100
override fun onFling(downEvent: MotionEvent, moveEvent: MotionEvent, velocityX: Float, velocityY: Float): Boolean
{
var result = false
val diffY = moveEvent.y - downEvent.y
val diffX = moveEvent.x - downEvent.x
if (Math.abs(diffX) > Math.abs(diffY))
{
// right or left swipe
if (Math.abs(diffX) > swipeThreshold && Math.abs(velocityX) > swipeVelocityThreshold)
{
if (diffX > 0)
{
swipeRight()
} else
{
swipeLeft()
}
result = true
}
} else
{
// up or down swipe
if (Math.abs(diffY) > swipeThreshold && Math.abs(velocityY) > swipeVelocityThreshold)
{
if (diffY > 0)
{
swipeDown()
} else
{
swipeUp()
}
result = true
}
}
return result
}
private fun swipeUp()
{
Toast.makeText(this, "Swipe Top", Toast.LENGTH_LONG).show()
slotSwipe1()
}
private fun swipeDown()
{
Toast.makeText(this, "Swipe Bottom", Toast.LENGTH_LONG).show()
slotSwipeMinus1()
}
private fun swipeLeft()
{
Toast.makeText(this, "Swipe Left", Toast.LENGTH_LONG).show()
slotSwipe0A()
}
private fun swipeRight()
{
Toast.makeText(this, "Swipe Right", Toast.LENGTH_LONG).show()
slotSwipe0B()
}
}
我知道抽象类不能被实例化,但是如何才能不报错地运行呢?这就是我来这里的原因。请帮忙
最佳答案
您必须实现GestureDetector.OnGestureListener
方法:
class dock : AppCompatActivity(), GestureDetector.OnGestureListener {
override fun onShowPress(e: MotionEvent?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onSingleTapUp(e: MotionEvent?): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onDown(e: MotionEvent?): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onScroll(e1: MotionEvent?, e2: MotionEvent?, distanceX: Float, distanceY: Float): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onLongPress(e: MotionEvent?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
(...)
提示:将光标指向“class dock”顶部,然后按 Alt+Enter 键,它应该会显示一个选项 Implement Members
关于android - 类不是抽象的/Activity 不能被实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57760160/