0

真正令人讨厌的部分是我的代码运行良好。现在,不知从何而来,它给了我这个错误:

    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,那么它就行不通了!知道为什么会这样吗?

4

3 回答 3

0

您正在初始化number为 null ,然后继续使用它而不检查它是否仍然为 null 。问题是,如果没有消息的原始地址等于您指定的地址,则number保持不变。要么初始化number为空字符串,要么在第 55 行尝试引用它之前检查它是否仍然为空

于 2013-02-27T12:04:39.243 回答
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));
}
于 2013-02-28T05:31:11.220 回答
0

我仍然不明白问题是什么。我一起创建了一个新项目,效果很好。

于 2013-03-07T08:42:38.013 回答