2017-04-12 52 views
-1

我想创建一个联系人应用程序到目前为止这么好,(我是Android开发新手),但我努力与代码的这一部分,我有AlertDialog,我想要的元素显示(联系人的信息,选中),带我到我的活动,(ActivitySMS.class准确),但虽然它似乎没有问题,运行应用程序,并从AlertDialog中选择出现的号码,它结束。日志没有显示值得探索和理解问题的地方。AlertDialog和意图

public class AgendaActivity extends ListActivity { 

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

    ContentResolver resolver = getContentResolver(); 
    Cursor mCursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 

    String[] contacts = { 
      ContactsContract.Contacts.DISPLAY_NAME_PRIMARY, 
      ContactsContract.Contacts.LOOKUP_KEY, 
      ContactsContract.Contacts._ID, 
    }; 

    int[] views = new int[]{ 
      android.R.id.text1 
    }; 

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
      android.R.layout.simple_list_item_2, 
      mCursor, 
      contacts, 
      views, 
      0); 

    // Bind to our new adapter. 
    setListAdapter(adapter); 

} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 

    final Cursor phoneCursor = getContentResolver().query(
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
      null, 
      ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", 
      new String[]{"" + id}, 
      null); 

    assert phoneCursor != null; 
    final int nTelephones = phoneCursor.getCount(); 
    final String[] telephones = new String[nTelephones]; 
    int x = 0; 

    while (phoneCursor.moveToNext()) { 
     int col = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
     telephones[x++] = phoneCursor.getString(col); 
    } 

    //Cursor Close 
    phoneCursor.close(); 

    final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Telephone Selection"); 
    builder.setItems(telephones, new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialogInterface, int item) { 

      String currentNumber = "smsto: " + (telephones[item]); 

      Intent sentIntent = new Intent(AgendaActivity.this, ActivitySMS.class); 
      sentIntent.setData(Uri.parse(currentNumber)); 



      startActivity(sentIntent); 
     } 
    }); 

    AlertDialog alert = builder.create(); 
    alert.show(); 
} 

有没有办法克服这个障碍?

+0

还是你应该张贴你的日志。 –

回答

0

通过

public static final String EXTRA_CURRENT_NUMBER = "EXTRA_CURRENT_NUMBER"; //just a string constant, it can be whatever you want 
... 
sendIntent.putExtra(EXTRA_CURRENT_NUMBER, currentNumber); 

替换行sendIntent.setData(Uri.parse(currentNumber));以后,您可以读取该值在您的活动这样

//ActivitySMS.class 
getIntent().getExtras().getString(MyDialogClass.EXTRA_CURRENT_NUMBER, "some default value...");