我正在构建一个电话应用程序,我可以在其中键入数字,或者选择一个名称并获取数字。
成功将数字输入标签,但如果我“返回/取消”联系人列表,它会崩溃。我很确定这是while循环,但我无法弄明白。
这是“获取号码”代码:
public void onActivityResult(int reqCode, int resultCode, Intent data) {
Uri uri = data.getData();
Cursor cursor=this.getContentResolver().query(uri, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(cursor.getString( cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
// You now have the number so now query it like this
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(
phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
numberfield.setText(phoneNumber);
}
phones.close();
}
}
}
整个.Java代码
public class Tabs extends Activity implements OnClickListener, OnLongClickListener{
TabHost th;
TabSpec specs;
TextView numberfield;
public String string;
public String number;
//public String phoneNumber;
@Override
protected void onCreate(Bundle savedInstanceState){
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
th = (TabHost)findViewById(R.id.tabhost);
numberfield = (TextView) findViewById(R.id.etNumberField);
Button n1 = (Button) findViewById (R.id.bNumber1);
Button n2 = (Button) findViewById (R.id.bNumber2);
Button n3 = (Button) findViewById (R.id.bNumber3);
Button n4 = (Button) findViewById (R.id.bNumber4);
Button n5 = (Button) findViewById (R.id.bNumber5);
Button n6 = (Button) findViewById (R.id.bNumber6);
Button n7 = (Button) findViewById (R.id.bNumber7);
Button n8 = (Button) findViewById (R.id.bNumber8);
Button n9 = (Button) findViewById (R.id.bNumber9);
Button nstar = (Button) findViewById (R.id.bNumberStar);
Button n0 = (Button) findViewById (R.id.bNumber0);
Button nhash = (Button) findViewById (R.id.bNumberHash);
Button call = (Button) findViewById (R.id.bCall);
Button sms = (Button) findViewById (R.id.bSMS);
Button clear = (Button) findViewById (R.id.bClear);
Button contact = (Button) findViewById (R.id.bContact);
n1.setOnClickListener(this);
n2.setOnClickListener(this);
n3.setOnClickListener(this);
n4.setOnClickListener(this);
n5.setOnClickListener(this);
n6.setOnClickListener(this);
n7.setOnClickListener(this);
n8.setOnClickListener(this);
n9.setOnClickListener(this);
nstar.setOnClickListener(this);
n0.setOnClickListener(this);
n0.setOnLongClickListener(this);
nhash.setOnClickListener(this);
call.setOnClickListener(this);
clear.setOnClickListener(this);
clear.setOnLongClickListener(this);
sms.setOnClickListener(this);
contact.setOnClickListener(this);
th.setup();
specs = th.newTabSpec("tag1");
specs.setContent(R.id.Recents);
specs.setIndicator("Recents");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.Keypad);
specs.setIndicator("Keypad");
th.addTab(specs);
specs = th.newTabSpec("tag3");
specs.setContent(R.id.Sms);
specs.setIndicator("SMS");
th.addTab(specs);
specs = th.newTabSpec("tag4");
specs.setContent(R.id.Ratings);
specs.setIndicator("Ratings");
th.addTab(specs);
specs = th.newTabSpec("tag5");
specs.setContent(R.id.Account);
specs.setIndicator("Account");
th.addTab(specs);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.bNumber1:
numberfield.setText(numberfield.getText() + "1");
break;
case R.id.bNumber2:
numberfield.setText(numberfield.getText() + "2");
break;
case R.id.bNumber3:
numberfield.setText(numberfield.getText() + "3");
break;
case R.id.bNumber4:
numberfield.setText(numberfield.getText() + "4");
break;
case R.id.bNumber5:
numberfield.setText(numberfield.getText() + "5");
break;
case R.id.bNumber6:
numberfield.setText(numberfield.getText() + "6");
break;
case R.id.bNumber7:
numberfield.setText(numberfield.getText() + "7");
break;
case R.id.bNumber8:
numberfield.setText(numberfield.getText() + "8");
break;
case R.id.bNumber9:
numberfield.setText(numberfield.getText() + "9");
break;
case R.id.bNumberStar:
numberfield.setText(numberfield.getText() + "*");
break;
case R.id.bNumber0:
numberfield.setText(numberfield.getText() + "0");
break;
case R.id.bNumberHash:
numberfield.setText(numberfield.getText() + "#");
break;
case R.id.bClear:
String number = numberfield.getText().toString();
if(number.length() > 0){
String newNumber = number.substring(0, number.length()-1);
numberfield.setText(newNumber);
}
break;
case R.id.bCall:
call();
break;
case R.id.bContact:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
}
}
private void call() {
// TODO Auto-generated method stub
String number = numberfield.getText().toString();
if(number.length() > 0){
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
string = numberfield.getText().toString().trim();
number = "tel:" + string;
callIntent.setData(Uri.parse(number));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("helloandroid dialing example", "Call failed");
}
}
}
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.bClear:
numberfield.setText("");
break;
case R.id.bNumber0:
numberfield.setText(numberfield.getText() + "+");
break;
}
return true;
}
public void onActivityResult(int reqCode, int resultCode, Intent data) {
Uri uri = data.getData();
Cursor cursor=this.getContentResolver().query(uri, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(cursor.getString( cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
// You now have the number so now query it like this
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(
phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
numberfield.setText(phoneNumber);
}
phones.close();
}
}
}
}
logcat的:
提前致谢!真的很赞赏
答案 0 :(得分:2)
在继续进行之前,您必须先检查resultCode
是否为RESULT_OK
。
您的onActivityResult
方法应该是这样的
public void onActivityResult(int reqCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && data != null) {
Uri uri = data.getData();
Cursor cursor=this.getContentResolver().query(uri, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(cursor.getString( cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
// You now have the number so now query it like this
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(
phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
numberfield.setText(phoneNumber);
}
phones.close();
}
}
}
}
有关onActivityResult
的详情,请参阅:onActivityResult
答案 1 :(得分:1)
当用户取消您为结果启动的活动时,它仍会调用onActivityResult,但数据将为null。您在onActivityResult中获得了空指针异常,因为您尝试取消引用空数据。如果活动被取消,您需要检查resultCode并跳过处理数据。
if (resultCode == RESULT_CANCELED) {
// respond appropriately
}
编辑:为了澄清,RESULT_CANCELED是在Activity中定义的常量。