我想编写一个应用程序以在等待接收短信时显示进度对话框。接收短信时,非空的 TextView smsReceive 作为关闭进度对话框的条件。但它得到runtimeException。你能告诉我在代码中要改变什么吗?
package cx.opseng.PerfTOLD;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.TextView;
public class Sms extends Activity {
/** Called when the activity is first created. */
static TextView smsReceive;
static TextView smsReceiveChar;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sms);
Intent i = getIntent();
// Receiving the Data
String reg = i.getStringExtra("reg");
String port = i.getStringExtra("port");
String smsMessage = reg + port ;
// Show SMS sent message on screen
TextView smsSend = (TextView) findViewById(R.id.smsSend);
smsSend.setText(smsMessage);
Log.i("smsSend",String.valueOf(smsSend.getText()));
// Send SMS message
SmsManager sm = SmsManager.getDefault();
String number = "5556";
sm.sendTextMessage(number, null, smsMessage, null, null);
ProgressDialog dialog = new ProgressDialog(Sms.this);
dialog.setMessage("working hard......");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
// Receive SMS message
smsReceive = null;
smsReceive = (TextView) findViewById(R.id.smsReceive);
Log.i("smsReceive",smsReceive.getText().toString());
if (smsReceive.getText().toString() != null) {
Log.i("smsReceive",smsReceive.getText().toString());
String smsChar = smsReceive.getText().toString();
Integer smsChar2 = smsChar.length();
String smsChar3 = Integer.toString(smsChar2);
Log.i("smsReceive2",smsChar3);
smsReceiveChar.setText(smsChar);
smsReceiveChar = (TextView) findViewById(R.id.smsReceiveChar);
}
if (smsReceiveChar.getText().toString() .equals("Message")) {
dialog.dismiss();
}
}
public static void updateMessageBox(String msg)
{
smsReceive.append(msg);
smsReceiveChar.append(msg);
}
}
public class SmsReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent)
{
Bundle bundle=intent.getExtras();
Object[] messages=(Object[])bundle.get("pdus");
SmsMessage[] sms = new SmsMessage[messages.length];
for(int n=0;n<messages.length;n++){
sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
}
for(SmsMessage msg:sms){
String num = msg.getOriginatingAddress();
Log.i("SMS sender",num);
if (num.equals("15555215556")) {
Sms.updateMessageBox("\nFrom: " + msg.getOriginatingAddress() +
"\n" + "Message: " + msg.getMessageBody() + "\n"); }
}
}
}
日志猫如下:
08-14 11:16:56.152: E/AndroidRuntime(2091): FATAL EXCEPTION: main
08-14 11:16:56.152: E/AndroidRuntime(2091): java.lang.RuntimeException: Unable to start activity ComponentInfo{cx.opseng.PerfTOLD/cx.opseng.PerfTOLD.Sms}: java.lang.StringIndexOutOfBoundsException
08-14 11:16:56.152: E/AndroidRuntime(2091): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-14 11:16:56.152: E/AndroidRuntime(2091): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-14 11:16:56.152: E/AndroidRuntime(2091): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-14 11:16:56.152: E/AndroidRuntime(2091): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-14 11:16:56.152: E/AndroidRuntime(2091): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 11:16:56.152: E/AndroidRuntime(2091): at android.os.Looper.loop(Looper.java:123)
08-14 11:16:56.152: E/AndroidRuntime(2091): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-14 11:16:56.152: E/AndroidRuntime(2091): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 11:16:56.152: E/AndroidRuntime(2091): at java.lang.reflect.Method.invoke(Method.java:521)
08-14 11:16:56.152: E/AndroidRuntime(2091): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-14 11:16:56.152: E/AndroidRuntime(2091): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-14 11:16:56.152: E/AndroidRuntime(2091): at dalvik.system.NativeStart.main(Native Method)
08-14 11:16:56.152: E/AndroidRuntime(2091): Caused by: java.lang.StringIndexOutOfBoundsException
08-14 11:16:56.152: E/AndroidRuntime(2091): at java.lang.String.substring(String.java:1579)
08-14 11:16:56.152: E/AndroidRuntime(2091): at cx.opseng.PerfTOLD.Sms.onCreate(Sms.java:88)
08-14 11:16:56.152: E/AndroidRuntime(2091): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-14 11:16:56.152: E/AndroidRuntime(2091): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-14 11:16:56.152: E/AndroidRuntime(2091): ... 11 more