在Android Studio中用Kotlin开发,安装后,启动是APP闪退了,日志如下: ---------------------------- PROCESS STARTED (20357) for package com.example.activitylifecycletest ---------------------------- 2024-08-13 18:03:27.603 20357-20377 HwActivityThreadImpl com.example.activitylifecycletest I before connect app.socketName:c73de58a-5183-41ed-a5f1-34f830a18725 2024-08-13 18:03:27.606 20357-20378 ActivityThread com.example.activitylifecycletest I Init compatible state: true 2024-08-13 18:03:27.607 20357-20378 HwFramewor...rtsFactory com.example.activitylifecycletest D HwFrameworkSecurityPartsFactory in. 2024-08-13 18:03:27.607 20357-20378 HwFramewor...rtsFactory com.example.activitylifecycletest I add HwFrameworkSecurityPartsFactory to memory. 2024-08-13 18:03:27.607 20357-20378 BehaviorCollectManager com.example.activitylifecycletest D hook process for AI virus second 2024-08-13 18:03:27.609 20357-20378 BehaviorCollectManager com.example.activitylifecycletest I bindAnalyzerService: huawei.android.security.IAppBehaviorDataAnalyzer$Stub$Proxy@260ba2c pid:20357 2024-08-13 18:03:27.634 20357-20357 ApplicationLoaders com.example.activitylifecycletest D createClassLoader zip: /data/app/com.example.activitylifecycletest-9Py_U4qoIkgdgyCgflox6g==/base.apk librarySearchPath: /data/app/com.example.activitylifecycletest-9Py_U4qoIkgdgyCgflox6g==/lib/arm64 libraryPermittedPath: /data/user/0/com.example.activitylifecycletest parent: java.lang.BootClassLoader@bf0bbd1 targetSdkVersion: 34 isBundled: false classLoaderName: null sharedLibraries: null 2024-08-13 18:03:27.779 20357-20357 tylifecycletes com.example.activitylifecycletest I HwMultiThreadDexLoader location /data/app/com.example.activitylifecycletest-9Py_U4qoIkgdgyCgflox6g==/base.apk start threadNum=3 2024-08-13 18:03:27.801 20357-20377 HwActivityThreadImpl com.example.activitylifecycletest E connect Error. 2024-08-13 18:03:27.802 20357-20377 HwActivityThreadImpl com.example.activitylifecycletest W closeStream error. 2024-08-13 18:03:27.802 20357-20377 HwActivityThreadImpl com.example.activitylifecycletest I connectContinuePreload end mPreloadStatus 3 2024-08-13 18:03:27.803 20357-20377 RtgSchedEvent com.example.activitylifecycletest I mRtgVideoEnable:false 2024-08-13 18:03:27.875 20357-20378 HwApiCacheMangerEx com.example.activitylifecycletest I apicache path=/storage/emulated/0 state=mounted key=com.example.activitylifecycletest#10510#256 2024-08-13 18:03:27.875 20357-20378 HwApiCacheMangerEx com.example.activitylifecycletest I need clear apicache,because volumes changed,oldCnt=0 newCnt=1 2024-08-13 18:03:27.876 20357-20378 HwApiCacheMangerEx com.example.activitylifecycletest I apicache path=/storage/emulated/0 state=mounted key=com.example.activitylifecycletest#10510#0 2024-08-13 18:03:27.876 20357-20378 HwApiCacheMangerEx com.example.activitylifecycletest I need clear apicache,because volumes changed,oldCnt=1 newCnt=1 2024-08-13 18:03:27.878 20357-20378 AwareBitmapCacher com.example.activitylifecycletest I init processName:com.example.activitylifecycletest pid=20357 uid=10510 2024-08-13 18:03:27.879 20357-20401 AwareLog com.example.activitylifecycletest E AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@354d18 2024-08-13 18:03:27.890 20357-20357 HwTypeface com.example.activitylifecycletest I updateFont: scale=1.0 2024-08-13 18:03:27.890 20357-20357 SystemFonts com.example.activitylifecycletest D areFontsVariable: script=Hant 2024-08-13 18:03:27.890 20357-20357 ActivityThread com.example.activitylifecycletest I finishPreloaded preloadStatus 3 2024-08-13 18:03:27.891 20357-20357 ActivityThread com.example.activitylifecycletest I finishPreloaded end preloadStatus 3 2024-08-13 18:03:27.891 20357-20357 RmeSchedManager com.example.activitylifecycletest I init Rme, version is: v1.0 2024-08-13 18:03:27.891 20357-20357 RmeSchedManager com.example.activitylifecycletest I init Rme, version is: v1.0 2024-08-13 18:03:27.903 20357-20357 OpenGLRenderer com.example.activitylifecycletest D disableOutlineDraw is true 2024-08-13 18:03:27.904 20357-20403 OpenGLRenderer com.example.activitylifecycletest I RenderThread: progress name = [com.example.activitylifecycletest], viewCornerType = 0 2024-08-13 18:03:27.905 20357-20405 iGraphics com.example.activitylifecycletest I [0020080c] pn: com.example.activitylifecycletest, p: 20357 2024-08-13 18:03:27.905 20357-20405 iGraphics com.example.activitylifecycletest I [0030080c] no spt app: com.example.activitylifecycletest 2024-08-13 18:03:27.938 20357-20357 AppCompatDelegate com.example.activitylifecycletest D Checking for metadata for AppLocalesMetadataHolderService : Service not found 2024-08-13 18:03:27.950 20357-20357 ActivityThread com.example.activitylifecycletest V callActivityOnCreate 2024-08-13 18:03:27.966 20357-20357 MainActivity信息 com.example.activitylifecycletest D onCreate 2024-08-13 18:03:27.975 20357-20357 DecorView[] com.example.activitylifecycletest I pkgName:com.example.activitylifecycletest old windowMode:0 new windoMode:1, isFixedSize:false 2024-08-13 18:03:27.982 20357-20357 BlockMonitor com.example.activitylifecycletest I dispatchingThrewException In MainThread 2024-08-13 18:03:27.982 20357-20357 AndroidRuntime com.example.activitylifecycletest D Shutting down VM 2024-08-13 18:03:27.982 20357-20357 QarthLog com.example.activitylifecycletest I [PatchStore] createDisableExceptionQarthFile 2024-08-13 18:03:27.982 20357-20357 QarthLog com.example.activitylifecycletest I [PatchStore] create disable file for com.example.activitylifecycletest uid is 10510 2024-08-13 18:03:27.983 20357-20357 AndroidRuntime com.example.activitylifecycletest E FATAL EXCEPTION: main Process: com.example.activitylifecycletest, PID: 20357 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activitylifecycletest/com.example.activitylifecycletest.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4071) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4258) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2624) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8679) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109) Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:926) at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:889) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:772) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:197) at com.example.activitylifecycletest.MainActivity.onCreate(MainActivity.kt:17) at android.app.Activity.performCreate(Activity.java:8214) at android.app.Activity.performCreate(Activity.java:8202) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4044) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4258) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2624) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8679) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109) 2024-08-13 18:03:27.988 20357-20357 Process com.example.activitylifecycletest I Sending signal. PID: 20357 SIG: 9 ---------------------------- PROCESS ENDED (20357) for package com.example.activitylifecycletest ---------------------------- 2024-08-13 18:03:28.101 1678-2805 WindowManager system_server E win=Window{6d7c9ad u0 Splash Screen com.example.activitylifecycletest EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true
请分析原因,并修复源代码。
根据日志信息,应用程序在启动时崩溃,主要原因是 MainActivity
没有使用 Theme.AppCompat
主题或其子类。具体的错误信息如下:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
这个错误通常是由于在 AndroidManifest.xml
中没有为 MainActivity
指定合适的主题,或者在代码中没有正确设置主题。
AndroidManifest.xml
文件确保在 AndroidManifest.xml
文件中为 MainActivity
指定了一个 Theme.AppCompat
主题或其子类。通常情况下,你可以使用 Theme.AppCompat.Light.DarkActionBar
或其他自定义的 AppCompat 主题。
<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/AppTheme">
<activity android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
styles.xml
文件确保在 res/values/styles.xml
文件中定义了一个继承自 Theme.AppCompat
的主题。例如:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
MainActivity
的 onCreate
方法确保在 MainActivity
的 onCreate
方法中没有覆盖主题设置。通常情况下,你不需要在代码中显式设置主题,但如果你有特殊需求,可以在 onCreate
方法中设置主题。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Ensure the theme is set before calling super.onCreate
setTheme(R.style.AppTheme_NoActionBar)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
以下是一个完整的示例,展示了如何在 AndroidManifest.xml
和 styles.xml
中正确设置主题,以及 MainActivity
的 onCreate
方法。
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitylifecycletest">
<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/AppTheme">
<activity android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
res/values/styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
MainActivity.kt:
package com.example.activitylifecycletest
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Ensure the theme is set before calling super.onCreate
setTheme(R.style.AppTheme_NoActionBar)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
通过以上修改,应该可以解决应用程序启动时的崩溃问题。