真正令人讨厌的部分是我的代码运行良好。现在,不知从何而来,它给了我这个错误:
02-28 14:49:43.086: E/AndroidRuntime(449): FATAL EXCEPTION: main
02-28 14:49:43.086: E/AndroidRuntime(449): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.warningalert/com.example.warningalert.WarningAlert}: java.lang.ClassCastException: com.example.warningalert.WarningAlert
02-28 14:49:43.086: E/AndroidRuntime(449): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-28 14:49:43.086: E/AndroidRuntime(449): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-28 14:49:43.086: E/AndroidRuntime(449): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-28 14:49:43.086: E/AndroidRuntime(449): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-28 14:49:43.086: E/AndroidRuntime(449): at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 14:49:43.086: E/AndroidRuntime(449): at android.os.Looper.loop(Looper.java:123)
02-28 14:49:43.086: E/AndroidRuntime(449): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-28 14:49:43.086: E/AndroidRuntime(449): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 14:49:43.086: E/AndroidRuntime(449): at java.lang.reflect.Method.invoke(Method.java:507)
02-28 14:49:43.086: E/AndroidRuntime(449): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-28 14:49:43.086: E/AndroidRuntime(449): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-28 14:49:43.086: E/AndroidRuntime(449): at dalvik.system.NativeStart.main(Native Method)
02-28 14:49:43.086: E/AndroidRuntime(449): Caused by: java.lang.ClassCastException: com.example.warningalert.WarningAlert
02-28 14:49:43.086: E/AndroidRuntime(449): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-28 14:49:43.086: E/AndroidRuntime(449): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-28 14:49:43.086: E/AndroidRuntime(449): ... 11 more
我的接收方法如下:
public class WarningAlert extends Activity {
private SmsReceiver receiver;
public static class SmsReceiver extends BroadcastReceiver{
final SmsManager sms = SmsManager.getDefault();
String body = null;
String no = "1555215556";
String number = null;
int i;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Parse the SMS.
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
// Retrieve the SMS.
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (i=0; i<msgs.length; i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
if(msgs[i].getOriginatingAddress().equals(no))
{
body = msgs[i].getMessageBody();
number = msgs[i].getOriginatingAddress();
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
if(no.equals(number.toString())){
Intent open = new Intent(context, StartAction.class);
open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
open.putExtra("body", body);
open.putExtra("number", number);
context.startActivity(open);
}
}
}
}
我的清单是:-
<receiver android:name="com.example.warningalert.WarningAlert$SmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.VIBRATE"/>
由于此代码最初运行良好,我想清单很好。
好吧..我有问题!问题出在嵌套类上。如果我直接在类 WarningAlert 中扩展 BroadcastReceiver 并在其中包含代码,它就可以工作。但是如果我在WarningAlert 类中有一个嵌套类SmsReceiver,那么它就行不通了!知道为什么会这样吗?