2013-04-01 73 views
0

我想提出一個應用程序,其中我正在訪問電話簿然後選擇一個聯繫人發送短信到特定的收件人,但每當我我正試圖發送一條短信獲取錯誤信息,這是我在Catch代碼塊中給出的,以防出現故障:「」短信失敗,請稍後重試!「」發送短信到選定的聯繫人[recepient]已經存儲在電話簿

注:我已經在真機上試過這也,並獲得同樣的問題存在,同樣也適合。

Contact.java ::

public interface Contact { 

public int getId(); 

public String getName(); 

public List<RawContact> getRawContacts(); 

}

BirthdayEditor.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) 

的Manifest.xml:

我已經給權限發送短信

<uses-permission 
    android:name="android.permission.SEND_SMS" /> 
+0

你在你的AndroidManifest.xml中''添加權限SEND_SMS' '文件? – Bishan

+0

@Bishan是的,我已經給出了,現在檢查我的問題 –

+0

從BirthdayEditor.java類的第106行發生錯誤。第106行的代碼是什麼? – Bishan

回答

1

@JimmyHill

不過你得到recepient的名字,因爲你正在使用getName()您的Contact.java類的方法如下行:

String recepient = contact.getName().toString();  

但要獲得在地名使用getRawContacts()你只需要更換上述線以下之一,因爲通過這個你可以在你的代碼得到聯繫人列表中的原始聯繫,只是數量檢查Contact.java一次:

String recepient = contact.getRawContacts().toString(); 

我相信它會爲你工作,如果出現任何問題,讓我知道...

相關問題