我正在制作一个应用程序,在该应用程序中我正在访问电话簿,然后选择一个联系人向该特定收件人发送短信,但是每当我尝试发送短信时,我都会在我的代码的 Catch 块中给出错误消息失败案例:" "短信失败,请稍后再试!“”
注意:我也在真实设备上尝试过这个,并且在那里遇到同样的问题......以及。
联系.java::
public interface Contact {
public int getId();
public String getName();
public List<RawContact> getRawContacts();
}
生日编辑器.java::
public class BirthdayEditor extends Activity implements OnItemClickListener {
private static final int CREATE_BIRTHDAY = 0;
private static final int EDIT_BIRTHDAY = 1;
private static final int DELETE_BIRTHDAY = 2;
private ImageButton saveGreeting;
public static final String CONTACT_ID = "contact_id";
private static final int NO_CONTACT_ID = Integer.MIN_VALUE;
private TextView name;
private ListView list;
private Database db;
private Contact contact;
ImageButton buttonSend;
EditText textSMS;
private Map<String, AuthenticatorDescription> map = new LinkedHashMap<String, AuthenticatorDescription>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editor);
// bind GUI components
this.name = (TextView) findViewById(R.id.editor_name);
this.list = (ListView) findViewById(R.id.editor_list);
// check if contact id is valid
this.db = new Database(getContentResolver());
final int contactId = getIntent().getIntExtra(CONTACT_ID, NO_CONTACT_ID);
this.contact = this.db.getContact(contactId);
if (this.contact == null) {
finish();
}
this.name.setText(this.contact.getName());
// pre-load information about all account types
AuthenticatorDescription[] authTypes = AccountManager.get(this).getAuthenticatorTypes();
for (AuthenticatorDescription authDesc : authTypes) {
this.map.put(authDesc.type, authDesc);
}
// bind list events
this.list.setOnItemClickListener(this);
this.list.setOnCreateContextMenuListener(this);
// create the GUI
updateView();
buttonSend = (ImageButton) findViewById(R.id.buttonSend);
textSMS = (EditText) findViewById(R.id.editTextSMS);
buttonSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String recepient = contact.getName().toString();
Log.d(LOG_TAG, "onClick(" + recepient + ")");
String sms = textSMS.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(recepient, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
Logcat 说:
04-01 14:12:29.211: D/BirthdayEditor(950): ButtonSend onClick RECEPIENT Stephen
04-01 14:12:29.211: D/BirthdayEditor(950): ButtonSend onClick MESSAGE Congrats
04-01 14:12:29.251: W/System.err(950): java.lang.NullPointerException
04-01 14:12:29.261: W/System.err(950): at android.os.Parcel.readException(Parcel.java:1431)
04-01 14:12:29.261: W/System.err(950): at android.os.Parcel.readException(Parcel.java:1379)
04-01 14:12:29.261: W/System.err(950): at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:434)
04-01 14:12:29.261: W/System.err(950): at android.telephony.SmsManager.sendTextMessage(SmsManager.java:87)
04-01 14:12:29.261: W/System.err(950): at com.chr.tatu.sample.friendslist.contacts.BirthdayEditor$2.onClick(BirthdayEditor.java:110)
04-01 14:12:29.261: W/System.err(950): at android.view.View.performClick(View.java:4202)
04-01 14:12:29.272: W/System.err(950): at android.view.View$PerformClick.run(View.java:17340)
04-01 14:12:29.272: W/System.err(950): at android.os.Handler.handleCallback(Handler.java:725)
04-01 14:12:29.272: W/System.err(950): at android.os.Handler.dispatchMessage(Handler.java:92)
04-01 14:12:29.272: W/System.err(950): at android.os.Looper.loop(Looper.java:137)
04-01 14:12:29.272: W/System.err(950): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-01 14:12:29.272: W/System.err(950): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 14:12:29.272: W/System.err(950): at java.lang.reflect.Method.invoke(Method.java:511)
04-01 14:12:29.272: W/System.err(950): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-01 14:12:29.272: W/System.err(950): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
清单.xml:
我已授予发送短信的权限
<uses-permission
android:name="android.permission.SEND_SMS" />