2

我正在使用 SMS 发送应用程序。它在我的 4.0.4 LG optimus 3D 上运行良好,但在我按下发送按钮后,4.0.3 SGS2 出现了一点问题,应用程序崩溃。错误日志在这里..它应该在飞行模式下显示一个 toast 'radio off',但它崩溃了。在 4.0.4 设备上显示正确的祝酒词。发送,无线电关闭等。请参阅代码以供参考。

java.lang.NullPointerException
at android.os.Parcel.readException(Parcel.java:1333)
at android.os.Parcel.readException(Parcel.java:1281)
at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:698)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:113)
at com.bahxlabs.simplesms.MainActivity.sendSMS(MainActivity.java:85)
at com.bahxlabs.simplesms.MainActivity.access$0(MainActivity.java:48)
at com.bahxlabs.simplesms.MainActivity$1.onClick(MainActivity.java:41)
at android.view.View.performClick(View.java:3620)
at android.view.View$PerformClick.run(View.java:14292)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4507)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)`

我的代码:

Button btnSendSMS;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
    btnSendSMS.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            EditText editText = (EditText)findViewById(R.id.editText1);
            String phn = editText.getText().toString();
            EditText msgText = (EditText)findViewById(R.id.editText3);
            String msg = msgText.getText().toString();
            sendSMS(phn,msg);
        }
    });
}

发送短信方法:

@SuppressLint("NewApi")
private void sendSMS(String phoneNumber, String message)
{
    String SENT="SMS SENT";
    PendingIntent sentPI=PendingIntent.getBroadcast(this,0,new Intent(SENT),0);
    registerReceiver(new BroadcastReceiver()
    {
        public void onReceive(Context arg0, Intent arg1) 
        {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent",Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure",Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service",Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU",Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, 
    new IntentFilter(SENT));

    recreate();

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber,  null, message,sentPI,null);
}

希望能说明情况

4

0 回答 0