Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试以编程方式发送SMS时,DestinationAddress异常无效_Java_Android - Fatal编程技术网

Java 尝试以编程方式发送SMS时,DestinationAddress异常无效

Java 尝试以编程方式发送SMS时,DestinationAddress异常无效,java,android,Java,Android,用户应在“edixxx”编辑文本中输入电话号码,我获取该号码并尝试向其发送“MSGHOSHARON”字符串,但当我点击按钮时,会出现以下异常:“无效的DestinationAddress” 有什么想法吗?我想不出这段代码有什么问题!:( 正在寻求帮助 package com.sms.validation; import android.app.Activity; import android.content.Intent; import android.content.SharedPrefere

用户应在“edixxx”编辑文本中输入电话号码,我获取该号码并尝试向其发送“MSGHOSHARON”字符串,但当我点击按钮时,会出现以下异常:“无效的DestinationAddress”

有什么想法吗?我想不出这段代码有什么问题!:(

正在寻求帮助

package com.sms.validation;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class acadmin extends Activity {


private SharedPreferences sphala;
public String enterd;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tanzadmin);

    final String msghoshharon="admin123456";


    sphala = getSharedPreferences("myPrefs",
            MODE_PRIVATE);
    final String number = (sphala.getString("number", ""));

    EditText text = (EditText)findViewById(R.id.edixxx);
    enterd = text.getText().toString();

    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            sendSMS(enterd, msghoshharon);
        }
    });

}

public void sendSMS(String enterd, String msghoshharon) {
    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(enterd, null, msghoshharon, null, null);
        Toast.makeText(getApplicationContext(), "has been sent",
                Toast.LENGTH_LONG).show();
    } catch (Exception ex) {
        Toast.makeText(getApplicationContext(), ex.getMessage().toString(),
                Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    }
}
}
试试这个

    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.telephony.SmsManager;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    public class acadmin extends AppCompatActivity implements View.OnClickListener {


    private SharedPreferences sphala;
    public String enterd;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tanzadmin);

        final String msghoshharon="admin123456";


        sphala = getSharedPreferences("myPrefs",
                MODE_PRIVATE);
        final String number = (sphala.getString("number", ""));

        EditText text = (EditText)findViewById(R.id.edixxx);


        final Button button = (Button) findViewById(R.id.button);


    }

    public void sendSMS(String enterd, String msghoshharon) {
        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(enterd, null, msghoshharon, null, null);
            Toast.makeText(getApplicationContext(), "has been sent",
                    Toast.LENGTH_LONG).show();
        } catch (Exception ex) {
            Toast.makeText(getApplicationContext(), ex.getMessage().toString(),
                    Toast.LENGTH_LONG).show();
            ex.printStackTrace();
        }
    }

 @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
            enterd = text.getText().toString();
                sendSMS(enterd, msghoshharon);
                break;
        }
    }
    }

enterd=text.getText().toString();
移动到
onClick()的内部
方法。现在,您在用户有机会输入任何内容之前保存EditText的文本,因此它会给您一个空字符串。您还必须将
输入的
设置为类字段,或
最终的
。更正:您还必须将
文本
设置为类字段,或
最终的
。您没有设置de>OnClickListener用于
按钮
,而
文本
onClick()
中是不可访问的,就像您现在使用的那样。