[英]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.文件结构:所有类
MainActivity
、 AlarmDialog
和AlarmReceiver
都在同一目录下。
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.