您好我正在开发一个程序,该程序获取用户的当前位置并将其发送给其中一个用户首选联系人。现在一些用户可能关闭了他们的位置,所以我添加了一个对话框提醒,要求他们按是启用他们的位置。单击是时,程序崩溃,我收到错误:
Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.michael79.handydoc/com.example.michael79.handydoc.emergencyActivity}: java.lang.NullPointerException: uri
这是我的代码:
public class emergencyActivity extends AppCompatActivity implements LocationListener {
private static final int REQUEST_CODE =1;
TextView t1;
LocationManager locationManager;
String mprovider;
//Context context=this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emergency);
t1=(TextView) findViewById(R.id.textContact);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
mprovider = locationManager.getBestProvider(criteria, false);
displayLocationSettingsRequest(this);
if (mprovider != null && !mprovider.equals("")) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location location = locationManager.getLastKnownLocation(mprovider);
locationManager.requestLocationUpdates(mprovider, 15000, 1, this);
if (location != null)
onLocationChanged(location);
else
Toast.makeText(getBaseContext(), "No Location Provider Found Check Your Code", Toast.LENGTH_SHORT).show();
}
}
public void getContact(View v){
Uri uri = Uri.parse("content://contacts");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(resultCode != Activity.RESULT_CANCELED) {
Uri uri = intent.getData();
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
Cursor cursor = getContentResolver().query(uri, projection,
null, null, null);
cursor.moveToFirst();
int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberColumnIndex);
int nameColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String name = cursor.getString(nameColumnIndex);
Toast.makeText(getApplicationContext(), "Number" + number + " Name" + name, Toast.LENGTH_LONG).show();
t1.setText(number);
//Log.d(TAG, "ZZZ number : " + number +" , name : "+name);
}
};
public void sendAlert(View v){
String phoneNo = t1.getText().toString();
String sms = "I need your help please i'm in distress";
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, 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();
}
}
@Override
public void onLocationChanged(Location location) {
TextView cord = (TextView) findViewById(R.id.textView26);
cord.setText("Current Longitude:" + location.getLongitude()+" Current Latitude:"+location.getLatitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
private void displayLocationSettingsRequest(Context context) {
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API).build();
googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(10000 / 2);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.i("show", "All location settings are satisfied.");
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i("show", "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");
try {
// Show the dialog by calling startResolutionForResult(), and check the result
// in onActivityResult().
status.startResolutionForResult(emergencyActivity.this, REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
Log.i("show", "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i("show", "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
break;
}
}
});
}
我检查了如何解决问题,但解决方法我没有解决它们。
答案 0 :(得分:1)
private static final int REQUEST_CODE =5;
private static final int STATUS_REQUEST_CODE =2;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode!=RESULT_CANCELED) {
if(requestCode==REQUEST_CODE) {
Uri uri = intent.getData();
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
Cursor cursor = getContentResolver().query(uri, projection,
null, null, null);
cursor.moveToFirst();
int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberColumnIndex);
int nameColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String name = cursor.getString(nameColumnIndex);
Toast.makeText(getApplicationContext(), "Number" + number + " Name" + name, Toast.LENGTH_LONG).show();
t1.setText(number);
//Log.d(TAG, "ZZZ number : " + number +" , name : "+name);
}
}
};
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.i("show", "All location settings are satisfied.");
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i("show", "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");
try {
// Show the dialog by calling startResolutionForResult(), and check the result
// in onActivityResult().
status.startResolutionForResult(emergencyActivity.this, STATUS_REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
Log.i("show", "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i("show", "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
break;
}
}
});
}
经过一段时间的阅读以及测试和修复后,我为status.startResolutionForResult()
创建了一个请求代码并更改了修复我的问题的onActivityResult()
中的条件语句