简体   繁体   English

Android:无法找到显式活动 class,即使我在 AndroidManifest.xml 中定义了它?

[英]Android: Unable to find explicit activity class even though I defined it in AndroidManifest.xml?

I am writing a simple application that triggers an alarm 5 seconds after the app launch, in which another activity (AlarmDialog) is launched.我正在编写一个简单的应用程序,它在应用程序启动 5 秒后触发警报,其中启动了另一个活动(AlarmDialog)。 However, when I run my app, I get the following error:但是,当我运行我的应用程序时,出现以下错误:

Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.basicalarmsetter/AlarmDialog};原因:android.content.ActivityNotFoundException:无法找到显式活动 class {com.example.basicalarmsetter/AlarmDialog}; have you declared this activity in your AndroidManifest.xml?您是否在 AndroidManifest.xml 中声明了此活动?

This seems strange, considering that I believe I have declared the activity in my AndroidManifest.xml file :这似乎很奇怪,考虑到我相信我已经在我的AndroidManifest.xml 文件中声明了该活动:

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

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name=".AlarmDialog"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        </activity>

        <receiver android:name=".AlarmReceiver"/>
    </application>

</manifest>

Below is my code for the app's other classes:以下是我的应用程序其他类的代码:

MainActivity.java : MainActivity.java

package com.example.basicalarmsetter;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {
    private Context context;

    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;

    // Creates the Pending Intent used to set off the alarm
    private PendingIntent generateAlarmPendingIntent(Context context) {
        Intent intent = new Intent(context, AlarmReceiver.class);

        // Each alarm requires a unique id
        int alarmId = (int) (Math.random() * (10000 - 1 + 1) + 1);
        System.out.println("alarmId = " + alarmId);

        PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(context, alarmId, intent, 0);
        return alarmPendingIntent;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = getApplicationContext();

        // Set an alarm 5 seconds after now
        alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        alarmMgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000,
                generateAlarmPendingIntent(context));
    }
}

AlarmReceiver :警报接收器

package com.example.basicalarmsetter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context k1, Intent k2) {
        System.out.println("Alarm received!");

        Intent i = new Intent();
        i.setClassName("com.example.basicalarmsetter", "AlarmDialog");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        k1.startActivity(i);
    }
}

AlarmDialog :警报对话框

package com.example.basicalarmsetter;

import android.app.Activity;
import android.os.Bundle;

public class AlarmDialog extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alarm_dialog);
    }
}

File Structure : All classes MainActivity , AlarmDialog , and AlarmReceiver , are under the same directory.文件结构:所有类MainActivityAlarmDialogAlarmReceiver都在同一目录下。

Replace:代替:

        Intent i = new Intent();
        i.setClassName("com.example.basicalarmsetter", "AlarmDialog");

with:和:

        Intent i = new Intent(k1, AlarmDialog.class);

This will use a better constructor, one that helps avoid the mistake in your hard-coded strings.这将使用更好的构造函数,有助于避免硬编码字符串中的错误。 As a bonus, if you use an IDE to rename the AlertDialog class, your Intent will be modified as well.作为奖励,如果您使用 IDE 重命名AlertDialog ,您的Intent也会被修改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法找到明确的活动类。 在 AndroidManifest.xml 中 - Unable to find explicit activity class. in AndroidManifest.xml Android Studio:无法找到显式活动 class 但活动已在 AndroidManifest.xml 中声明 - Android Studio: Unable to find explicit activity class but activity already declared in AndroidManifest.xml 无法找到显式活动 class {}; 你有没有在你的 AndroidManifest.xml 地图中声明这个活动 API 不工作g;(Java ZE846DZDBC9AB870 - Unable to find explicit activity class {}; have you declared this activity in your AndroidManifest.xml Maps API not workingg ;( Java Android Studio? android.content.ActivityNotFoundException:无法找到显式活动 class {您是否在 AndroidManifest.xml 中声明了此活动? - android.content.ActivityNotFoundException: Unable to find explicit activity class { have you declared this activity in your AndroidManifest.xml? android.content.ActivityNotFoundException:无法找到显式活动类; 您是否在 AndroidManifest.xml 中声明了此活动? - android.content.ActivityNotFoundException: Unable to find explicit activity class; have you declared this activity in your AndroidManifest.xml? 无法找到明确的活动类,是否已在AndroidManifest.xml中声明了该活动? - Unable to find explicit activity class have you declared this activity in your AndroidManifest.xml? 屏幕锁定导致无法找到明确的活动类,您是否已在AndroidManifest.xml中声明了该活动? - Screen Locked causing Unable to find explicit activity class have you declared this activity in your AndroidManifest.xml? 无法找到显式活动类 {}; 您是否在 AndroidManifest.xml 中声明了此活动 - Unable to find explicit activity class {}; have you declared this activity in your AndroidManifest.xml 错误,即使活动存在于AndroidManifest.xml中 - Fault that the activity is not in AndroidManifest.xml even though it's there 即使存在明确的活动类别也无法找到 - Unable to find explicit activity class even though it exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM