非法目标地址短信管理员共享首选项

时间:2017-12-30 20:15:48

标签: java android

我使用存储在共享首选项中的数字来发送短信但是当我运行应用程序时它会崩溃并且logcat说有一个非法的目标地址如何?

这是我的logcat

12-30 21:01:30.758 12239-12239/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.android.beez.help2, PID: 12239
                                               java.lang.IllegalArgumentException: Invalid destinationAddress
                                                   at android.telephony.SmsManager.sendTextMessage(SmsManager.java:127)
                                                   at com.android.beez.help2.MainActivity.sendSms(MainActivity.java:63)
                                                   at com.android.beez.help2.MainActivity$2.onClick(MainActivity.java:41)
                                                   at android.view.View.performClick(View.java:4475)
                                                   at android.view.View$PerformClick.run(View.java:18790)
                                                   at android.os.Handler.handleCallback(Handler.java:808)
                                                   at android.os.Handler.dispatchMessage(Handler.java:103)
                                                   at android.os.Looper.loop(Looper.java:193)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5328)
                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
                                                   at dalvik.system.NativeStart.main(Native Method)

这是我的

  

MainActivity.java

package com.android.beez.help2;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
Button button;
SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonInit();
    Button setupMa = (Button) findViewById(R.id.setupMA);
    setupMa.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent a = new Intent(MainActivity.this,Setup.class);
            startActivity(a);
        }
    });


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            loadCredit();
            sendSms();

        }
    });
}


protected void sendSms() {

    spInit();

    String number1 = sharedPreferences.getString("first", "");
    String number2 = sharedPreferences.getString("second", "");
    String number3 = sharedPreferences.getString("third", "");
    String name = sharedPreferences.getString("name", "");
    String text = "Help, this is " + name + ", if you are reading this I am in trouble please help me" +
            " Iam located at " + "http://www.google.com/maps/place/"+GPSTracker.latitude+","+GPSTracker.longitude+ " " +
            "" +
            "" +
            "-Sent via the Emergency App";

    SmsManager manager = SmsManager.getDefault();
    manager.sendTextMessage(number1, null, text, null, null);
    manager.sendTextMessage(number2, null, text, null, null);
    manager.sendTextMessage(number3, null, text, null, null);



    boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
            .getBoolean("isFirstRun", true);

    if (isFirstRun) {
        Intent launchSetups = new Intent(MainActivity.this, Setup.class);
        startActivity(launchSetups);


        }
    }

protected void loadCredit() {

    spInit();

    String creditLine = sharedPreferences.getString("dialLoadSp","");

    Uri number = Uri.parse("tel:"+creditLine);
    Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
    startActivity(callIntent);
}

public void buttonInit() {
     button = (Button) findViewById(R.id.button_main);
}
public void spInit() {
    sharedPreferences = MainActivity.this.getSharedPreferences("com.android.beez.help2",MODE_PRIVATE);

}

}

发送消息的数字来自editTexts,并存储在sharedPreferences中。问题可能来自共享偏好,还是文本管理器的问题

2 个答案:

答案 0 :(得分:0)

我认为您应该指定destination address添加if ... if(num1!="" || num2!="" || num3...) manager.sendText...

答案 1 :(得分:0)

哦,我知道这一个:)希望我不迟到。

如果IllegalArgumentExceptiondestinationAddress为空,则抛出

text。 在您的情况下,您有这个String number1 = sharedPreferences.getString("first", "");,这意味着您的number1在应用程序首次运行时为空,因此您会收到该异常。 另外,您在这里没有提供完整的代码,而是在其中说数字将由编辑文本填充。