如何解决java.lang.RuntimeException 错误:无法实例化活动 ComponentInfo KOTLIN/AndroidStudio
我正在尝试使用 Kotlin 和 Android Studio 制作 Space Invaders 的副本,以了解有关此类开发的更多信息,但发生此错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.spaceinvaders,PID: 3982
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.spaceinvaders/com.example.spaceinvaders.GameView}: java.lang.InstantiationException: java.lang.class<com.example.spaceinvaders.GameView> has no zero argument constructor
at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2914)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6864)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.InstantiationException: java.lang.class<com.example.spaceinvaders.GameView> has no zero argument constructor
at java.lang.class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1216)
at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2902)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6864)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
我有两项活动:一项用于菜单,一项用于游戏。当我尝试通过单击按钮从一个菜单传递到另一个菜单时,它崩溃了。 这里有其他代码:
主屏幕:
class HomeScreen : Activity(),View.OnClickListener{
lateinit var nbAliens : TextView
lateinit var SPACE_SHIP : Bitmap //L'image choisi par le joueur
var ALIENS_NUMBER : Int = 0 //Le nombre d'ennemi choisi par le joueur
var flagOK : Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_ecran_accueil)
nbAliens = findViewById(R.id.textViewNbEnnemis)
btnMinus.setonClickListener(this)
btnPlus.setonClickListener(this)
imgBtnSpaceShip1.setonClickListener(this)
imgBtnSpaceShip2.setonClickListener(this)
btnStart.setonClickListener(this)
}
override fun onClick(v: View?) {
when (v){
btnMinus ->{
//Modify the textView et change ALIEN_NUMBER
var i = nbAliens.text.toString().toInt()
if (i > 7) i--
nbAliens.text = i.toString()
ALIENS_NUMBER = i
}
btnPlus ->{
var i = nbAliens.text.toString().toInt()
if (i < 21) i++
nbAliens.text = i.toString()
ALIENS_NUMBER = i
}
imgBtnSpaceShip1 ->{
/*When you click in the imageButton,you chose your SpaceShip Image
The first one's background goes green while the other becomes all grey
*/
val color = Color.argb(130,255,255)
imgBtnSpaceShip1.setColorFilter(Color.TRANSPARENT)
imgBtnSpaceShip2.setBackgroundColor(color)
imgBtnSpaceShip2.setColorFilter(color)
imgBtnSpaceShip1.setBackgroundColor(Color.GREEN)
SPACE_SHIP = BitmapFactory.decodeResource(resources,R.drawable.space_ship1)
flagOK = true
}
imgBtnSpaceShip2 ->{
val color = Color.argb(130,255)
imgBtnSpaceShip2.setColorFilter(Color.TRANSPARENT)
imgBtnSpaceShip1.setBackgroundColor(color)
imgBtnSpaceShip1.setColorFilter(color)
imgBtnSpaceShip2.setBackgroundColor(Color.GREEN)
SPACE_SHIP = BitmapFactory.decodeResource(resources,R.drawable.space_ship2)
flagOK = true
}
btnStart -> {
//Change from HomeScreen to GameView
if (flagOK) {
val intent: Intent =
Intent(applicationContext,GameView::class.java)
startActivity(intent)
finish()
}else {
Toast.makeText(this,"You have to pick a spaceship to start!",Toast.LENGTH_LONG).show()
}
}
}
}
}
游戏视图:
class GameView @JvmOverloads constructor (context: Context,var imgSpaceShip: Bitmap,attributes: AttributeSet? = null,defStyleAttr: Int = 0): View(context,attributes,defStyleAttr){
private lateinit var imgalienGreen : Bitmap
private lateinit var imgalienRed : Bitmap
private lateinit var imgalienWhite : Bitmap
private var alien_margin by Delegates.notNull<Float>()
private var alien_side by Delegates.notNull<Float>()
val game : Game = Game()
val backgroundPaint = Paint()
//Load images and resize it
override fun onSizeChanged(w: Int,h: Int,oldw: Int,oldh: Int) {
super.onSizeChanged(w,h,oldw,oldh)
alien_margin = width*0.025f
alien_side = (width - (game.NB_ALIENS_COLUMNS+1)*alien_margin)/game.NB_ALIENS_COLUMNS
//There is another class Game for the logic of the game
try{
imgalienGreen = BitmapFactory.decodeResource(resources,R.drawable.alien_green)
imgalienGreen = Bitmap.createScaledBitmap(imgalienGreen,alien_side.toInt(),true)
imgalienRed = BitmapFactory.decodeResource(resources,R.drawable.alien_red)
imgalienRed = Bitmap.createScaledBitmap(imgalienRed,true)
imgalienWhite = BitmapFactory.decodeResource(resources,R.drawable.alien_white)
imgalienWhite = Bitmap.createScaledBitmap(imgalienWhite,true)
imgSpaceShip = Bitmap.createScaledBitmap(imgSpaceShip,true)
}catch (exception : Exception) {
Log.e("ERROR","Cannot load images");
}
}
//Build the rectangles where the images will be set on.
private fun alienRectF(index: Int) : RectF{
val x : Float = alien_margin + alien_side
val y : Float = height + 0.17f
return RectF(x,y,x+alien_side,y+alien_side)
}
//It finishes here but I will continue coding after resolving it
}
我不知道发生了什么。感谢您的帮助!
解决方法
要使用startActivity函数,你应该从Activity导航到Activity,但GameView不是Activity,它是一个View(从View扩展,但应该是Activity)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。