应用程序在4.0.3崩溃但在4.0.4上正常工作

时间:2013-04-12 20:35:02

标签: java android 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);
        }
    });
}

SendSMS方法:

@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);
}

希望它澄清情况

0 个答案:

没有答案