2012-11-08 38 views
1

您好,我试图设置基于输入到SQL数据库的通知。当我尝试保存输入的数据时,出现错误AlarmReciever Activity Not Found;Android活动未找到广播接收器

当我在下面添加到清单,我收到了类似的错误无法找到明确的类我本来这个清单中的

<receiver android:process=":remote" android:name="AlarmReciever"></receiver> 

。任何建议将不胜感激。

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

     </activity> 

Addcourse类片断

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类

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) 
+0

您是否得到了答案?我面临同样的问题。 – Vibhav

回答

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

+0

这是我最初的样子,但我仍然收到同样的错误。 – Dinah

0

您在AndroidManifest.xml文件同一封装内登记ActivityBroadcastReceiver具有相同名称的所以用活动名称,而不是用于注册活动的BroadcastReceiver类

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

很抱歉,但这并不奏效。我仍然得到相同的错误 – Dinah

0

试试这并保持接收者的名称与活动不同。

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

    </activity> 
+0

我试过你的建议,但得到了错误:11-08 12:37:23.328:E/AndroidRuntime(227):未捕获的处理程序:由于未捕获的异常导致主线程退出 11-08 12:37: 23.348:E/AndroidRuntime(227):java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.mycoursetimetable/com.example.mycoursetimetable.AlarmReciever}:java.lang.ClassCastException:com.example.mycoursetimetable.AlarmReciev – Dinah

+0

将整个logcat粘贴到pastebin中并在此处给出链接 – PravinCG