checkSelfPermission空指针6.0 [英] checkSelfPermission nullpointer 6.0

查看:542
本文介绍了checkSelfPermission空指针6.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个检查permisions问题总是说我的空指针在调试6.0:

我在做什么错

 公共字符串getGeolocation(上下文的背景下){    尝试{
        INT MY_PERMISSION_ACCESS_COURSE_LOCATION = 1000;        如果(ContextCompat.checkSelfPermission(上下文,android.Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){            ActivityCompat.requestPermissions(这一点,新的String [] {} android.Manifest.permission.ACCESS_COARSE_LOCATION,
                    MY_PERMISSION_ACCESS_COURSE_LOCATION);
        }
        如果(Build.VERSION.SDK_INT> = 23安培;&安培;
                ContextCompat.checkSelfPermission(背景下,android.Manifest.permission.ACCESS_FINE_LOCATION)= PackageManager.PERMISSION_GRANTED和放大器;!&安培;
                ContextCompat.checkSelfPermission(上下文,android.Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
            //返回0;
        }
        mLocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE); //这里是空指针
        //地点位置= mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        地点= mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        如果(!位置= NULL&放大器;&安培; location.getTime()> Calendar.getInstance()getTimeInMillis() - 2 * 60 * 1000){
            //做一些与最近的位置锁定
            //否则等待下面的更新
        }
        其他{
            //mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,这一点);
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,这一点);
        }
    }赶上(例外五){}
返回地球;}

//我用这个方法来获得数据:

 公共无效onLocationChanged(地点){
    如果(位置!= NULL){
        Log.v(位置发生改变,location.getLatitude()+和+ location.getLongitude());
        缘=+ location.getLatitude()+,+ location.getLongitude();
        mLocationManager.removeUpdates(本);
    }
}

这是Android的工作室错误异常:

  java.lang.IllegalStateException:之前的onCreate不向活动系统服务()

在哪里我叫getGeolocation()?

 公共类AlarmReceiverCustom扩展广播接收器{
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
getGeolocation(上下文)


解决方案

您正在给一个上下文 getGeolocation(),还是你打电话 getSystemService()对象本身。

您可能需要使用您传递的上下文:

  mLocationManager =(的LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

但你必须要考虑的<一个href=\"https://developer.android.com/reference/android/content/BroadcastReceiver.html#ReceiverLifecycle\"相对=nofollow>接收机和流程生命周期太:


  

一个BroadcastReceiver对象仅适用于呼叫的持续时间的onReceive(上下文,意图)。一旦这个函数的code返回时,系统会认为这个对象被完成并不再有效。 [...]


  
  

这意味着,对于长时间运行的操作,你会经常一起使用服务与一个BroadcastReceiver来保持包含进程的积极为您的操作的全部时间。


所以你最好重新实现应用程序的这一部分,使用服务来更新位置。

I have a problem with a check permisions always says nullpointer my debug in 6.0:

what am I doing wrong

public String getGeolocation(Context context){

    try{        
        int MY_PERMISSION_ACCESS_COURSE_LOCATION = 1000;         

        if ( ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {

            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSION_ACCESS_COURSE_LOCATION);
        }
        if ( Build.VERSION.SDK_INT >= 23 &&
                ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            //return 0 ;
        }
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // Here is nullpointer
        //Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Location location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
            // Do something with the recent location fix
            //  otherwise wait for the update below
        }
        else {
            //mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,  0, 0, this);
        }
    }catch (Exception e){}
return geo;

}

// I use this method to get data :

public void onLocationChanged(Location location) {
    if (location != null) {
        Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude());
        geo = ""+location.getLatitude() + " , " + location.getLongitude();
        mLocationManager.removeUpdates(this);
    }
}

Error exception from Android studio :

java.lang.IllegalStateException: System services not available to Activities before onCreate()

Where I call getGeolocation() ?

public class AlarmReceiverCustom extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
getGeolocation(context)

解决方案

You're giving a context to getGeolocation(), still you're calling getSystemService() on the object itself.

You might want to use the context you passed:

mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

But you have to consider the Receiver and Process Lifecycle too:

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active. [...]

This means that for longer-running operations you will often use a Service in conjunction with a BroadcastReceiver to keep the containing process active for the entire time of your operation.

So you better reimplement this part of your application and use a Service to update the location.

这篇关于checkSelfPermission空指针6.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆