致命异常:main java.lang.RuntimeException:无法启动活动ComponentInfo{com



我是安卓开发的初学者,我使用kotlin(最低API 21(开发一款带有飞溅屏幕、主页活动、游戏活动的井字游戏。

所有活动的img[1]

在家庭活动中单击"播放"按钮后,我的应用程序崩溃。该应用程序崩溃,称";不幸的是,应用程序已停止";当我在模拟器中运行时。并显示错误AndroidRuntime:致命异常:main java.lang.RuntimeException:无法在我的logcat中启动活动ComponentInfo。请帮助我修复这个错误,因为我是kotlin和安卓的新手

请帮忙!

MainActivity.kt sorce代码

package com.naman.tictactoe
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.view.WindowManager
@Suppress("DEPRECATION")
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// This is used to hide the status bar and make
// the splash screen as a full screen activity.
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
// we used the postDelayed(Runnable, time) method
// to send a message with a delayed time.
Handler().postDelayed({
val intent = Intent(this, HomeActivity::class.java)
startActivity(intent)
finish()
}, 3000) // 3000 is the delayed time in milliseconds.
}}

HomeActivity.kt sorce代码

package com.naman.tictactoe
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.content.Intent
import android.view.View

class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)

}
fun play(view: View) {
val intent = Intent(this, GameActivity::class.java)
startActivity(intent)
}
}

GameActivity.kt sorcecode

package com.naman.tictactoe
import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageButton
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.res.ResourcesCompat
import com.naman.tictactoe.R.layout.activity_game

class GameActivity : AppCompatActivity() {
private lateinit var buttons : Array<Array<ImageButton>>
private lateinit  var textViewPlayer1: TextView
private lateinit var textViewPlayer2: TextView
private var player1Turn: Boolean = true
private var roundCount: Int = 0
private var player1Point: Int = 0
private var player2Point: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(activity_game)
textViewPlayer1 = findViewById(R.id.player1TextView)
textViewPlayer2 = findViewById(R.id.player2TextView)

buttons = Array(3){r->
Array(3){c->
initButtons(r,c)
}
}
val btnReset: ImageButton = findViewById(R.id.btnReset)
btnReset.setOnClickListener{
player1Point = 0
player2Point = 0
clearBoard()
updateScore()
}
}
private fun initButtons(r: Int, c: Int): ImageButton {
val btn: ImageButton =
findViewById(resources.getIdentifier("btn$r$c","id",packageName))
btn.setOnClickListener{
onButtonClick(btn)
}
return btn
}
private fun onButtonClick(btn: ImageButton) {
if (btn.drawable != null) return
if (player1Turn){
btn.setImageResource(R.drawable.couple_edt_cross)
}else {
btn.setImageResource(R.drawable.couple_edt_heart)
}
roundCount++
if (checkForWin()){
if (player1Turn) win(1) else win(2)
} else if(roundCount == 9){
draw()
}else{
player1Turn = ! player1Turn
}
}
private fun checkForWin(): Boolean {
val field= Array(3){r->
Array(3){c->
getField(buttons[r][c])
}
}
for (i in 0..2){
if ((field[i][0] == field[i][1])&&
(field[i][0] == field[i][2])&&
(field[i][0] !=null)
)return true

if ((field[0][i] == field[1][i])&&
(field[0][i] == field[2][i])&&
(field[0][i] !=null)
)return true

if ((field[0][0] == field[1][1])&&
(field[0][0] == field[2][2])&&
(field[0][0] !=null )
)return true

if ((field[0][2] == field[1][1])&&
(field[0][2] == field[2][0])&&
(field[0][2] !=null )
)return true
}
return false
}
private fun getField(btn: ImageButton): Char? {
val drw: Drawable? = btn.drawable
val drwCross: Drawable? = ResourcesCompat.getDrawable(resources, R.drawable.couple_edt_cross,null)
val drwHeart: Drawable? = ResourcesCompat.getDrawable(resources, R.drawable.couple_edt_heart,null)
return when(drw?.constantState){
drwCross?.constantState -> 'X'
drwHeart?.constantState -> 'O'
else -> null
}
}

private fun win(player: Int) {
if (player == 1) player1Point++ else player2Point++
Toast.makeText(applicationContext,"Player $player Won!", Toast.LENGTH_SHORT).show()
updateScore()
clearBoard()
}
private fun clearBoard() {
for (i in 0..2) for (j in 0..2) {
buttons[i][j].setImageResource(0)
}
roundCount = 0
player1Turn = true
}
private fun updateScore() {
textViewPlayer1.text = "Player 1 : $player1Point"
textViewPlayer2.text = "Player 2 : $player2Point"
}

private fun draw(){
Toast.makeText(applicationContext,"Match Draw", Toast.LENGTH_SHORT).show()
clearBoard()
}

}

LogCat:

06-18 17:17:54.290 5994-5994/? I/art: Not late-enabling -Xcheck:jni (already on)
06-18 17:17:54.339 5994-5994/? W/ActivityThread: Application com.naman.tictactoe is waiting for the debugger on port 8100...
06-18 17:17:54.358 5994-5994/com.naman.tictactoe I/System.out: Sending WAIT chunk
06-18 17:17:54.499 5994-5999/com.naman.tictactoe I/art: Debugger is active
06-18 17:17:54.558 5994-5994/com.naman.tictactoe I/System.out: Debugger has connected
06-18 17:17:54.558 5994-5994/com.naman.tictactoe I/System.out: waiting for debugger to settle...
06-18 17:17:54.760 5994-5994/com.naman.tictactoe I/System.out: waiting for debugger to settle...
06-18 17:17:54.961 5994-5994/com.naman.tictactoe I/System.out: waiting for debugger to settle...
06-18 17:17:55.161 5994-5994/com.naman.tictactoe I/System.out: waiting for debugger to settle...
06-18 17:17:55.362 5994-5994/com.naman.tictactoe I/System.out: waiting for debugger to settle...
06-18 17:17:55.564 5994-5994/com.naman.tictactoe I/System.out: waiting for debugger to settle...
06-18 17:17:55.766 5994-5994/com.naman.tictactoe I/System.out: waiting for debugger to settle...
06-18 17:17:55.968 5994-5994/com.naman.tictactoe I/System.out: debugger has settled (1360)
06-18 17:17:55.972 5994-5994/com.naman.tictactoe W/System: ClassLoader referenced unknown path: 
/data/app/com.naman.tictactoe-1/lib/x86
06-18 17:17:56.008 5994-5994/com.naman.tictactoe W/art: Before Android 4.1, method 
android.graphics.PorterDuffColorFilter 
androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-18 17:18:08.308 5994-5994/com.naman.tictactoe I/art: Starting a blocking GC Alloc
06-18 17:18:08.308 5994-5994/com.naman.tictactoe I/art: Starting a blocking GC Alloc
06-18 17:18:08.316 5994-5994/com.naman.tictactoe I/art: Alloc sticky concurrent mark sweep GC 
freed 1766(187KB) AllocSpace objects, 0(0B) LOS objects, 25% free, 11MB/14MB, paused 625us total 8.367ms
06-18 17:18:08.317 5994-5994/com.naman.tictactoe I/art: Starting a blocking GC Alloc
06-18 17:18:08.327 5994-5994/com.naman.tictactoe I/art: Alloc partial concurrent mark sweep GC 
freed 379(22KB) AllocSpace objects, 0(0B) LOS objects, 26% free, 11MB/15MB, paused 618us total 10.127ms
06-18 17:18:08.327 5994-5994/com.naman.tictactoe I/art: Starting a blocking GC Alloc
06-18 17:18:08.339 5994-5994/com.naman.tictactoe I/art: Alloc concurrent mark sweep GC freed 85(15KB) AllocSpace objects, 0(0B) LOS objects, 26% free, 11MB/15MB, paused 634us total 11.276ms
06-18 17:18:08.339 5994-5994/com.naman.tictactoe I/art: Forcing collection of SoftReferences for 394MB allocation
06-18 17:18:08.339 5994-5994/com.naman.tictactoe I/art: Starting a blocking GC Alloc
06-18 17:18:08.347 5994-5994/com.naman.tictactoe I/art: Alloc concurrent mark sweep GC freed 1131(85KB) AllocSpace objects, 1(9MB) LOS objects, 43% free, 1357KB/2MB, paused 490us total 7.662ms
06-18 17:18:08.347 5994-5994/com.naman.tictactoe W/art: Throwing OutOfMemoryError "Failed to 
allocate a 414034332 byte allocation with 1048576 free bytes and 382MB until OOM"
06-18 17:18:08.347 5994-5994/com.naman.tictactoe I/art: Starting a blocking GC Alloc
06-18 17:18:08.347 5994-5994/com.naman.tictactoe I/art: Starting a blocking GC Alloc
06-18 17:18:08.351 5994-5994/com.naman.tictactoe I/art: Starting a blocking GC Alloc
06-18 17:18:08.355 5994-5994/com.naman.tictactoe I/art: Starting a blocking GC Alloc
06-18 17:18:08.360 5994-5994/com.naman.tictactoe I/art: Forcing collection of SoftReferences for 
394MB allocation
06-18 17:18:08.360 5994-5994/com.naman.tictactoe I/art: Starting a blocking GC Alloc
06-18 17:18:08.365 5994-5994/com.naman.tictactoe I/art: Alloc concurrent mark sweep GC freed 
3(96B) AllocSpace objects, 0(0B) LOS objects, 43% free, 1357KB/2MB, paused 330us total 5.247ms
06-18 17:18:08.365 5994-5994/com.naman.tictactoe W/art: Throwing OutOfMemoryError "Failed to allocate a 414034332 byte allocation with 1048576 free bytes and 382MB until OOM"
06-18 17:18:08.365 5994-5994/com.naman.tictactoe D/skia: --- allocation failed for scaled bitmap
06-18 17:18:08.368 5994-5994/com.naman.tictactoe D/AndroidRuntime: Shutting down VM


--------- beginning of crash
06-18 17:18:08.368 5994-5994/com.naman.tictactoe E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.naman.tictactoe, PID: 5994
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.naman.tictactoe/com.naman.tictactoe.GameActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class <unknown>
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.naman.tictactoe.GameActivity.onCreate(GameActivity.kt:26)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699) 
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195) 
at com.naman.tictactoe.GameActivity.onCreate(GameActivity.kt:26) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699) 
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195) 
at com.naman.tictactoe.GameActivity.onCreate(GameActivity.kt:26) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 414034332 byte allocation with 1048576 free bytes and 382MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
at android.content.res.Resources.loadDrawable(Resources.java:2540)
at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
at android.view.View.<init>(View.java:3948)
at android.view.ViewGroup.<init>(ViewGroup.java:573)
at android.widget.LinearLayout.<init>(LinearLayout.java:203)
at android.widget.LinearLayout.<init>(LinearLayout.java:199)
at android.widget.LinearLayout.<init>(LinearLayout.java:195)
at java.lang.reflect.Constructor.newInstance(Native Method) 
at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699) 
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195) 
at com.naman.tictactoe.GameActivity.onCreate(GameActivity.kt:26) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
06-18 17:18:10.650 5994-5994/? I/Process: Sending signal. PID: 5994 SIG: 9
'''

[1]: https://i.stack.imgur.com/MQNSG.png
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 414034332 byte allocation with 1048576 free bytes and 382MB until OOM

GameActivity布局的第9行有一个从LinearLayout引用的大位图,很可能是android:background。您可以使图像在像素分辨率方面更小,以减少内存需求。

在这个日志中,我们可以看到

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.naman.tictactoe/com.naman.tictactoe.GameActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class <unknown>

因此,在GameActivity布局的第9行添加了错误的标签。

相关内容

  • 没有找到相关文章