你瞅啥呢

2025-03-03 使用intlij创建Android(kotlin)应用,初始化完后点击运行app报错:error: resource mipmap/ic_launcher (aka com.example.clockApp:mipmap/ic_launcher) not found. ==》找不到mipmap/ic_launcher文件,需要手动添加

呃,其实我也是ds一下才知道我需要去创建一些icon资源,找到res文件夹,右键选中==》New==》Image Asset

 Icon type这个选项选择launch icons(Adaptive and Legacy),默认也是这个,选好了点击Next,

 然后点Finish

接着重新运行app,

以下不关“找不到mipmap/ic_launcher文件”的事,除非你创建项目的时候选择的是no activity。。

结果报错:Error running 'app' Default Activity not found

原因出在AndroidManifest.xml代码里,一下是默认生成的代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
>

    <application
            android:allowBackup="true"
            android:fullBackupContent="@xml/backup_rules"
            tools:targetApi="31"
            android:dataExtractionRules="@xml/data_extraction_rules"
            android:label="@string/app_name"
            android:icon="@mipmap/ic_launcher"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.ClockApp"/>

</manifest>

也许是因为我创建的时候选择的是no activity。。。

 解决方案:添加一个默认的 Activity 配置,比如:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.clockApp"> 

    <application
        android:allowBackup="true"
        android:fullBackupContent="@xml/backup_rules"
        tools:targetApi="31"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ClockApp">

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

还要新建一个类型MainActivity

package com.example.clockApp

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Greeting("Android")
        }
    }
}

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    Greeting("Android")
}

好了,重新build一下吧

 

posted @ 2025-03-03 14:46  叶乘风  阅读(276)  评论(0)    收藏  举报