java - 空对象引用上的 Location.getLatitude()

标签 java android parse-platform geolocation

我收到以下运行时错误: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“double android.location.Location.getLatitude()”

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.ListView;

import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {
    // Declare Variables
    ListView listview;
    List<ParseObject> ob;
    ProgressDialog mProgressDialog;
    ListViewAdapter adapter;
    Context context;
    private List<PlaceFilter> worldpopulationlist = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from listview_main.xml
        setContentView(R.layout.activity_main);
        // Execute RemoteDataTask AsyncTask
        new RemoteDataTask().execute();
    }

    // RemoteDataTask AsyncTask
    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(MainActivity.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Parse.com Custom ListView Tutorial");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();

这是 ParseQuery

 try {
                // Locate the class table named "Country" in Parse.com
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                        "geo_filters");



                        // Locate the column named "ranknum" in Parse.com and order list
                // by ascending

                ob=query.find();
                for (ParseObject PlaceLocation : ob) {
                    // Locate images in flag column
                    ParseFile image = (ParseFile) PlaceLocation.get("FilterFile");
                    PlaceFilter map = new PlaceFilter();
                    ParseGeoPoint userLocation = new ParseGeoPoint(latitude, longitude);
                    query.whereNear("PlaceLocation", userLocation); //column as location
                    query.whereWithinKilometers("PlaceLocation", userLocation, 1);
                    map.setPlaceName((String) PlaceLocation.get("PlaceName"));
                    map.setFilterFile(image.getUrl());
                    worldpopulationlist.add(map);
                }
            } catch (ParseException e){
                Log.e("Error",e.getMessage());
                e.printStackTrace();
            }

最佳答案

 Runtime Error: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference

位置为空,尚未初始化。

检查位置是否不为空:

if(location!= null){
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
}

此外,您应该将所有位置服务代码包装到 try and catch 中,并检查空指针引用和连接、GPS 提供商等。看看这里 BasicLocationSample .

关于java - 空对象引用上的 Location.getLatitude(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34498147/

相关文章:

javascript - 变量在方法之外不起作用

java - 如何写入没有属性名称的 JSON 值

java - 获取 adb 中显示的 Android 设备 ID

android - 在这种情况下,将 C/C++ 用于 iOS/Android 跨平台代码是否合理?

java - 与电子邮件地址的对话

ios - 从存储在 Parse 中的图像中获取 url

ios - 导航栏按钮未显示在 PFQueryTableViewController(解析 + iOS)中

java - 如何在 Java 应用程序中创建堆转储?

java - 64 位 java 上的 JMF?

java - 如何查询Ldap以获取结果中的用户属性并将其用于自动填写JSF2.0表单