I'm building a Phoning App, where I can either type the number, or select a name and get the number.
It succeeds on getting the number into the Label, but if I "Go back/Cancel" the contact list, It crashes. I'm pretty sure it's the while loop, but I can't figure it out.
Here's the "Get Number" code:
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();
}
}
}
Whole .Java Code
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:
Thanks in advance! Really Appriciated
You must check the if resultCode
is RESULT_OK
before going further. Your onActivityResult
method should be something like this
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();
}
}
}
}
For more info about onActivityResult
you can refer this : onActivityResult
When the user cancels an activity that you started for a result it still calls onActivityResult, but the data will be null. You're getting a null pointer exception inside onActivityResult because you try dereference the null data. You need to check resultCode and skip processing the data if the activity was cancelled.
if (resultCode == RESULT_CANCELED) {
// respond appropriately
}
edit: To clarify, RESULT_CANCELED is a constant defined in Activity.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.