Java – Why do I get ClassCastException?

Why do I get ClassCastException?… here is a solution to the problem.

Why do I get ClassCastException?

I came across ClassCastException while running this simple application.

It was my first attempt at AlarmManager.

public class AlarmReciever extends BroadcastReceiver {

@Override
     public void onReceive(Context context, Intent intent) {
       try {
         Bundle bundle = intent.getExtras();
         String message = bundle.getString("alarm_message");
         Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
         Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
         e.printStackTrace();

}
     }

}

Here is my list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.tcs.mine"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
         <activity android:name=". AlarmReciever"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  <receiver  android:process=":remote" android:name=". AlarmReceiver"></receiver>
 </application>
</manifest> 

What am I doing wrong?

Solution

AlarmReceiver is not an activity but is declared as one. See the documentation on BroadcastReceiver how to declare them in the manifest file this tutorial

Related Problems and Solutions