真正烦人的部分是我的代码工作得非常好。而现在,从不在哪里,它给了我这个错误:
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那么它就不会工作!任何想法为什么这个hapenning ??
答案 0 :(得分:0)
您正在将number
初始化为null,然后继续使用它,而不检查它是否仍为null。问题是,如果没有消息的起始地址等于您指定的地址,则number
保持不变。将number
初始化为空字符串,或在尝试在第55行引用它之前检查它是否仍然为空
答案 1 :(得分:0)
请使用此代码。
public void sendSMS(String phoneNumber, String message) {
mPhoneNumber = phoneNumber;
mMessage = message;
PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0, new Intent(DELIVERED), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(mPhoneNumber, null, mMessage, sentPI, deliveredPI);
Toast.makeText(mContext, "SMS Sending...", Toast.LENGTH_SHORT).show();
// ---when the SMS has been sent---final String string = "deprecation";
mContext.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(mContext, "SMS sent", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(mContext, "Generic failure", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(mContext, "No service", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(mContext, "Null PDU", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(mContext, "Radio off", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
// ---when the SMS has been delivered---
mContext.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(mContext, "SMS delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(mContext, "SMS not delivered", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
}
答案 2 :(得分:0)
我仍然不明白问题是什么。我一起创建了一个新项目,它运行良好。