android.content.ActivityNotFoundException:找不到显式活动类 com.example.collegeroamer.NavigationDisplayActivity

android.content.ActivityNotFoundException: Unable to find explicit activity class com.example.collegeroamer.NavigationDisplayActivity

提问人:Bhushan Savaratkar 提问时间:11/8/2023 最后编辑:Bhushan Savaratkar 更新时间:11/8/2023 访问量:24

问:

我在android studio中有一个kotlin/xml文件。我想使用图像按钮转到我的活动图像显示屏幕,但每当我选择位置和目的地,然后单击导航按钮时,应用程序就会停止工作

错误

Process: com.example.collegeroamer, PID: 10761
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.collegeroamer/com.example.collegeroamer.NavigationDisplayActivity}; have you declared this activity in your AndroidManifest.xml?
                                                                                                    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2065)
                                                                                                    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
                                                                                                    at android.app.Activity.startActivityForResult(Activity.java:5320)
                                                                                                    at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:728)
                                                                                                    at android.app.Activity.startActivityForResult(Activity.java:5278)
                                                                                                    at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:709)
                                                                                                    at android.app.Activity.startActivity(Activity.java:5664)
                                                                                                    at android.app.Activity.startActivity(Activity.java:5617)
                                                                                                    at com.example.collegeroamer.MainActivity.onCreate$lambda$0(MainActivity.kt:26)
                                                                                                    at com.example.collegeroamer.MainActivity.$r8$lambda$v-UY7dqHrHnhJePGV2sXZapuvnU(Unknown Source:0)
                                                                                                    at com.example.collegeroamer.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
                                                                                                    at android.view.View.performClick(View.java:7448)
                                                                                                    at android.view.View.performClickInternal(View.java:7425)
                                                                                                    at android.view.View.access$3600(View.java:810)
                                                                                                    at android.view.View$PerformClick.run(View.java:28305)
                                                                                                    at android.os.Handler.handleCallback(Handler.java:938)
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                    at android.os.Looper.loop(Looper.java:223)
                                                                                                    at android.app.ActivityThread.main(ActivityThread.java:7656)
                                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
                                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

我的代码

安卓清单.xml

<?xml version="1.0" encoding="utf-8"?>

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

MainActivity.kt (英语)

package com.example.collegeroamer

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageButton
import android.widget.Spinner

class MainActivity : AppCompatActivity() {
    private lateinit var navigateButton: ImageButton
    private lateinit var currentLocationSpinner: Spinner
    private lateinit var destinationSpinner: Spinner
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        navigateButton = findViewById(R.id.navigateButton)
        currentLocationSpinner = findViewById(R.id.currentLocationSpinner)
        destinationSpinner = findViewById(R.id.destinationSpinner)

        navigateButton.setOnClickListener {
            val selectedLocation = currentLocationSpinner.selectedItem.toString()

            val intent = Intent(this, NavigationDisplayActivity::class.java)
            intent.putExtra("selected_location", selectedLocation)
            startActivity(intent)
        }
    }
}

导航显示活动.kt

@file:Suppress("UNUSED_EXPRESSION")

package com.example.collegeroamer

import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity

class NavigationDisplayActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_image_display)
        val locationImage = findViewById<ImageView>(R.id.locationImage)

        val selectedLocation = intent.getStringExtra("selected_location")
        
        val imageResourceId = getImageResourceIdForLocation(selectedLocation ?: "")
        locationImage.setImageResource(imageResourceId)
    }

    private fun getImageResourceIdForLocation(location: String): Int {
        return when (location) {
            else -> R.drawable.default_image
        }
    }
}

Android XML Kotlin 运行时错误 imagebutton

评论


答:

0赞 Byte Code 11/8/2023 #1

您应该在 AndroidManifest.xml 中声明 NavigationDisplayActivity,如下所示。

    <application 
    ...
    <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>

    <activity
        android:name=".NavigationDisplayActivity" >
    </activity>
</application>