Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.NullPointerException:uri-通过警报对话框打开位置_Java_Android - Fatal编程技术网

java.lang.NullPointerException:uri-通过警报对话框打开位置

java.lang.NullPointerException:uri-通过警报对话框打开位置,java,android,Java,Android,嗨,我正在开发一个程序,它可以获取用户的当前位置,并将其发送给用户首选的联系人之一。现在一些用户可能已经关闭了他们的位置,所以我添加了一个对话框警报,要求他们通过按yes来启用他们的位置。单击“是”时,程序崩溃,我得到错误信息: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.michael79.

嗨,我正在开发一个程序,它可以获取用户的当前位置,并将其发送给用户首选的联系人之一。现在一些用户可能已经关闭了他们的位置,所以我添加了一个对话框警报,要求他们通过按yes来启用他们的位置。单击“是”时,程序崩溃,我得到错误信息:

 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;
            }
        }
    });
}
我检查了如何解决问题,但我得到的解决方案无法解决问题

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中的条件语句,该语句修复了我的问题

您需要为联系人的startActivityForResult调用使用不同的请求代码,以及位置设置的status.startResolutionForResult调用。然后,您需要在onActivityResult中检查该请求代码,以了解您得到的是哪个请求回调,并相应地采取行动,而不是无条件地查询那里的联系人提供商。好的,您能给我看一个示例代码吗?