vindaaron
vindaaron

Reputation: 97

Why do I get a ClassCastException?

I am getting a ClassCastException on running this simple application.

I'm trying to use the AlarmManager for the first time.

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();

        }
     }

    }

this is my manisfest:

<?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?

Upvotes: 3

Views: 327

Answers (2)

Fabrizio D&#39;Ammassa
Fabrizio D&#39;Ammassa

Reputation: 4769

The package statement is missing at the top of your java class.

Besides, check how the AlarmReceiver is spelled throughout your .java and .xml files. Somewhere is called AlarmReciever.

Upvotes: 0

Stephan
Stephan

Reputation: 7388

AlarmReceiver is not an Activity but declared as one. Check the documentation on BroadcastReceiver and how to declare them in the manifest file. Maybe you want to check this tutorial

Upvotes: 4

Related Questions