Android Activity未找到广播接收器

时间:2012-11-08 09:42:47

标签: android exception broadcastreceiver

您正在尝试根据输入到SQL数据库来设置通知。当我尝试保存输入的数据时,我收到错误AlarmReciever Activity Not Found;

我最初在清单

中有这个
<receiver  android:process=":remote" android:name="AlarmReciever"></receiver>

当我在清单中添加以下内容时,我得到类似的错误,无法找到显式类。任何建议都会非常感激。

<activity
            android:name=".AlamReciever"
            android:label="@string/title_activity_user_menu" >

        </activity>

Addcourse class snippet

public void onClick (View addModuleButton) 
            {

                String moduleCode = sqlModuleCode.getText().toString();
                String moduleName = sqlModuleName.getText().toString();

                String moduleType = spinnerModuleType.getSelectedItem().toString();
                String moduleDay = spinnerModuleDay.getSelectedItem().toString();

                String moduleStartHour = timePickerStart.getCurrentHour().toString();
                String moduleStartMinute = timePickerStart.getCurrentMinute().toString();

                String moduleEndHour = timePickerEnd.getCurrentHour().toString();
                String moduleEndMinute = timePickerEnd.getCurrentMinute().toString();

                String moduleLocation = sqlModuleLocation.getText().toString();
                String moduleComments = sqlModuleComments.getText().toString();


                MODULEDATABASE = new database(AddCourse.this);
                MODULEDATABASE.openToWrite();
                MODULEDATABASE.createEntry(moduleCode , moduleName, moduleType, moduleDay, moduleStartHour, 
                        moduleStartMinute, moduleEndHour, moduleEndMinute, moduleLocation, moduleComments);

                int Hour = Integer.parseInt(moduleStartHour);
                int Minute = Integer.parseInt(moduleStartMinute);

                Calendar cal = Calendar.getInstance();

                int currentDay = cal.get(Calendar.DAY_OF_WEEK);
                int setDay = 0;
                if(moduleDay=="Sunday")
                {
                    setDay = 1;
                }
                else if(moduleDay=="Monday")
                {
                    setDay = 2;
                }
                else if(moduleDay=="Tuesday")
                {
                    setDay = 3;
                }
                else if(moduleDay=="Wednesday")
                {
                    setDay = 4;
                }
                else if(moduleDay=="Thursday")
                {
                    setDay = 5;
                }
                else if(moduleDay=="Friday")
                {
                    setDay = 6;
                }
                else if(moduleDay=="Saturday")
                {
                    setDay = 7;
                }

                   cal.set(Calendar.DAY_OF_WEEK,4);
                   cal.set(Calendar.HOUR_OF_DAY,Hour);
                   cal.set(Calendar.MINUTE, Minute);
                   Intent intent = new Intent(AddCourse.this, AlarmReciever.class);
                   intent.putExtra("alarm_message",moduleName+ "is about to begin");
                   PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                   // Get the AlarmManager service
                   AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                   am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
                   long weekly = AlarmManager.INTERVAL_DAY * 7;
                am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),weekly,sender);
                startActivity(intent);

AlarmReciever Class

public class AlarmReciever extends BroadcastReceiver {

 @Override

     public void onReceive(Context context, Intent intent) {     
         NotificationCompat.Builder nb = new NotificationCompat.Builder(context);
         nb.setContentTitle("title");
         nb.setContentText("message");
         nb.setSmallIcon(R.drawable.ic_launcher);

         NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
         final Intent notificationIntent = new Intent(context, AddCourse.class);
         notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

         final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
         nb.setContentIntent(contentIntent);
         Notification notification = nb.getNotification();
         nm.notify(0, notification);
      }


}

目录下载

11-07 12:51:41.195: E/AndroidRuntime(3927): Uncaught handler: thread main exiting due to uncaught exception
11-07 12:51:41.216: E/AndroidRuntime(3927): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.mycoursetimetable/com.example.mycoursetimetable.AlarmReciever}; have you declared this activity in your AndroidManifest.xml?
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.app.Activity.startActivityForResult(Activity.java:2749)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.app.Activity.startActivity(Activity.java:2855)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at com.example.mycoursetimetable.AddCourse.onClick(AddCourse.java:169)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.view.View.performClick(View.java:2364)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.view.View.onTouchEvent(View.java:4179)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.view.View.dispatchTouchEvent(View.java:3709)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.os.Looper.loop(Looper.java:123)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at android.app.ActivityThread.main(ActivityThread.java:4363)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at java.lang.reflect.Method.invokeNative(Native Method)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at java.lang.reflect.Method.invoke(Method.java:521)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-07 12:51:41.216: E/AndroidRuntime(3927):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

如果您使用广播接收器,则应将其声明为接收器,而不是活动,并且通常应该具有与之关联的操作。像这样:

<receiver android:name=".myReceiverName" android:exported="true">
<intent-filter>
    <action android:name="android.intent.action.some_action"/>
    <data android:scheme="package" android:path="com.blah.mypackage">
</intent-filter>

有关详细说明,请参阅官方指南:

https://developer.android.com/guide/topics/manifest/receiver-element.html

答案 1 :(得分:0)

您正在Activity文件中的同一个包中注册BroadcastReceiverAndroidManifest.xml同名,因此请使用活动名称而不是BroadcastReceiver类来注册活动

        <activity
            android:name=".Your_Activity_Name"
            android:label="@string/title_activity_user_menu" >
        </activity>

答案 2 :(得分:0)

尝试此操作并保持接收者的名称与活动不同。

<activity
        android:name="com.example.mycoursetimetable.AlarmReciever"
        android:label="@string/title_activity_user_menu" >

    </activity>