Java 尝试以编程方式发送SMS时,DestinationAddress异常无效
用户应在“edixxx”编辑文本中输入电话号码,我获取该号码并尝试向其发送“MSGHOSHARON”字符串,但当我点击按钮时,会出现以下异常:“无效的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
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的文本,因此它会给您一个空字符串。您还必须将输入的
设置为类字段,或最终的
。更正:您还必须将文本
设置为类字段,或最终的
。您没有设置按钮
,而文本
在onClick()
中是不可访问的,就像您现在使用的那样。