未找到Android活动异常:找不到处理Intent的活动

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了未找到Android活动异常:找不到处理Intent的活动相关的知识,希望对你有一定的参考价值。

这似乎应该很容易修复,有几个帖子带有不同示例的示例代码,更复杂。然而,没有人为这个简单的计划工作过。

BMI计算器有两个活动,MainActivity和BMIResultsScreen。用户在MainActivity中输入他们的体重和身高,并在BMIResultsScreen中查看结果。然后,他们可以选择单击按钮退出,或单击按钮返回MainActivity。退出按钮有效,但返回MainActivity的按钮不起作用。

清单:

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

 <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".BMIResultsScreen">
            <intent-filter>
                <action android:name="com.example.username.BMIResultsScreen" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>

MainActivity:

  package com.example.username.bmicalculator

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*



class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    setOnClickListenerForButton()
}

fun calculateBMI(): Double {
    var weight: Double = (numWeight.getText().toString().toDouble())
    var height: Double = (numHeight.getText().toString().toDouble())
    var calculatedBMI = (((weight / height) / height))
    return calculatedBMI
}

fun setOnClickListenerForButton() {
    btnCalculateBMI.setOnClickListener {

        val intent = Intent("com.example.username.BMIResultsScreen")
        intent.putExtra("BMIResult", calculateBMI())
        startActivity(intent)
    }
}
}

第二项活动,BMIResultScreen:

 package com.example.username.bmicalculator

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.content.Intent
import kotlinx.android.synthetic.main.activity_bmiresults_screen.*
import java.text.DecimalFormat


class BMIResultsScreen : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bmiresults_screen)

        showBMIResult()
        findBMICategory()
        setExitListener()
        setCheckAgainListener()
    }

    fun showBMIResult(){
        var decFormat = DecimalFormat("#.#")
        var formattedBMI = decFormat.format(getIntent().getExtras().getDouble("BMIResult"))
        lblBMIResult.setText(formattedBMI.toString())
    }

    fun findBMICategory() {
        var categoryOfBMI = "Unknown"
        var resultBMI = getIntent().getExtras().getDouble("BMIResult")
        if (resultBMI < 15) {
            categoryOfBMI = "Very Severely Underweight"
        } else if (resultBMI in 15..16) {
            categoryOfBMI = "Severely Underweight"
        } else if (resultBMI > 16 && resultBMI <= 18.5) {
            categoryOfBMI = "Underweight"
        } else if (resultBMI > 18.5 && resultBMI <= 25) {
            categoryOfBMI = "Normal (Healthy Weight)"
        } else if (resultBMI in 25..30) {
            categoryOfBMI = "Overweight"
        } else if (resultBMI in 30..35) {
            categoryOfBMI = "Moderately Obese"
        } else if (resultBMI in 35..40) {
            categoryOfBMI = "Severely Obese"
        } else if (resultBMI >= 40) {
            categoryOfBMI = "Very Severely Obese"
        }
        lblBMIResultCategory.setText(categoryOfBMI)
    }

        fun setExitListener() {
            btnExit.setOnClickListener {
                this.finishAffinity()
            }
        }

            fun setCheckAgainListener() {
                btnCheckAgain.setOnClickListener {
                    val intent = Intent("com.example.username.bmicalculator.MainActivity")
                    startActivity(intent)
                }
            }
    }
答案

尝试在侦听器中调用onBackPressed()以返回返回Main活动的按钮。

          btnCheckAgain.setOnClickListener {
                this.onBackPressed()
            }

以上是关于未找到Android活动异常:找不到处理Intent的活动的主要内容,如果未能解决你的问题,请参考以下文章

Android:在已部署的应用程序中找不到资源异常

未处理的 JS 异常:找不到变量:需要

没有找到处理 Intent 的活动:android.intent.action.EDIT

未找到处理意图的活动

Android assets 找不到文件异常 FileNotFoundException

角度:发生未处理的异常:找不到模块'webpack'?