java.lang.RuntimeException 错误:无法实例化活动 ComponentInfo KOTLIN/AndroidStudio

时间:2021-04-17 05:26:19

标签: android kotlin android-activity execution runtimeexception

我正在尝试使用 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,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,255,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(), alien_side.toInt(), true)

            imgAlienRed = BitmapFactory.decodeResource(resources, R.drawable.alien_red)
            imgAlienRed = Bitmap.createScaledBitmap(imgAlienRed, alien_side.toInt(), alien_side.toInt(), true)

            imgAlienWhite = BitmapFactory.decodeResource(resources, R.drawable.alien_white)
            imgAlienWhite = Bitmap.createScaledBitmap(imgAlienWhite, alien_side.toInt(), alien_side.toInt(), true)

            imgSpaceShip = Bitmap.createScaledBitmap(imgSpaceShip, alien_side.toInt(), alien_side.toInt(), 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
}

我不知道发生了什么。感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

要使用startActivity函数,你应该从Activity导航到Activity,但GameView不是Activity,它是一个View(从View扩展,但应该是Activity)