为什么我会得到ClassCastException?

时间:2011-05-16 09:23:46

标签: java android

我正在运行这个简单的应用程序ClassCastException

我第一次尝试使用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();

        }
     }

    }

这是我的狂欢节:

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

我做错了什么?

2 个答案:

答案 0 :(得分:4)

AlarmReceiver不是Activity,而是声明为一个。查看BroadcastReceiverhow to declare them in the manifest file上的文档。也许您想查看this tutorial

答案 1 :(得分:0)

java类顶部缺少package语句。

此外,检查AlarmReceiver在.java和.xml文件中的拼写方式。某处叫做AlarmReciever。

相关问题