【问题标题】:Google Play Services Location API simple code not workingGoogle Play 服务位置 API 简单代码不起作用
【发布时间】:2018-11-18 09:14:36
【问题描述】:

我正在尝试获取用户的当前位置,但我的应用程序崩溃了,我只是不明白如何让它工作:

这是 Google Play 服务位置 API,我按照此处所示的说明进行操作:Location and context overview,但我的代码不起作用

    public class MainActivity extends AppCompatActivity {
    private FusedLocationProviderClient mFusedLocationClient;
    private LocationCallback mLocationCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            LocationRequest mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(10000);
            mLocationRequest.setFastestInterval(5000);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            startLocationUpdates(mLocationRequest);

            mLocationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                if (locationResult == null) {
                    return;
                }
                for (Location location : locationResult.getLocations()) {
                    TextView tv = (TextView)findViewById(R.id.tv);
                    Double latitude = location.getLatitude();
                    String latitudeString = location.toString();
                    tv.setText(latitudeString);
                }
            };
        };

    }

    private void startLocationUpdates(LocationRequest mLocationRequest) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            TextView tv = (TextView)findViewById(R.id.tv);
            tv.setText("Not Permitted");
        } else {
        mFusedLocationClient.requestLocationUpdates(mLocationRequest,
                mLocationCallback,
                null /* Looper */);
        }
    }

}

崩溃消息是:

尝试调用虚拟方法 'com.google.android.gms.tasks.Task com.google.android.gms.location.FusedLocationProviderClient.requestLocationUpdates(com.google.android.gms.location.LocationRequest, com.google. android.gms.location.LocationCallback, android.os.Looper)' 在空对象引用上

【问题讨论】:

  • code you please crash message

标签: android


【解决方案1】:

你忘记初始化 FusedLocationClient, 在你调用这个startLocationUpdates()之前,在你的onCreate中创建这个mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

【讨论】:

  • 现在我得到Unable to start activity ComponentInfo{com.example.locationer/com.example.locationer.MainActivity}: java.lang.NullPointerException: Listener must not be null。我相信它与这个“循环器”有关,我不知道它是做什么的,或者不是?您能否将代码复制到您的环境中,看看是不是我的电脑有问题?
  • 谢谢,请问是什么原因导致了这个问题?
  • 你的 'setContentView(R.layout.activity_main)' 在 init 之前是否融合?
  • 发布您的 xml 文件
【解决方案2】:

你没有初始化mFusedLocationClient

试试这个

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

}

【讨论】:

  • 添加这一行后,我得到一个不同的错误:Now I get Unable to start activity ComponentInfo{com.example.locationer/com.example.locationer.MainActivity}: java.lang.NullPointerException: Listener must not be null
  • @Fireio 发布你的完整堆栈
【解决方案3】:

首先
您应该在清单文件中添加权限

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

第二次
清除 FusedLoction 客户端并请求位置的运行时权限获取最后一个已知位置

private FusedLocationProviderClient mFusedLocationClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);



// request premission here and check out its result and it will work fine 
    mFusedLocationClient.getLastLocation()
        .addOnSuccessListener(this, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                // Got last known location. In some rare situations this can be null.
                if (location != null) {
                    // Logic to handle location object
                }
            }
    });
}

还可以快速查看 github 上的 class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    相关资源
    最近更新 更多