0

我的应用程序中有一个功能可以获取用户 gps 坐标,然后返回附近的啤酒厂。当我使用该功能时,它从未强制关闭,并且其他人已经对其进行了测试并且它有效。一位用户在打开获取用户位置并尝试获取啤酒厂位置的活动时报告了此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.beerportfolio.beerportfoliopro/com.example.beerportfoliopro.FindBrewery}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
at android.app.ActivityThread.access$600(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.beerportfoliopro.FindBrewery.onCreate(FindBrewery.java:42)
at android.app.Activity.performCreate(Activity.java:5066)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
... 11 more

我发起的活动是:

package com.example.beerportfoliopro;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

import com.beerportfolio.beerportfoliopro.R;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by mike on 7/3/13.
 */
public class FindBrewery extends ActionbarMenu {


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);




        setContentView(R.layout.beer_location_list);

        String title = "Nearby Breweries";
        TextView topTitle = (TextView) findViewById(R.id.beerLocationTitle);
        topTitle.setText(title);

        //get user location

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



        //construct url
        String url = myURLandKey;

        Log.d("urlTest",url);

        //async task goes here
        new GetNearbyBreweries(this).execute(url);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main2, menu);

        return true;
    }

}

最后我的异步任务是:

package com.example.beerportfoliopro;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.beerportfolio.beerportfoliopro.R;

public class GetNearbyBreweries extends AsyncTask
        <String, Void, String> {

    Context c;
    private ProgressDialog Dialog;

    public GetNearbyBreweries (Context context)
    {
        c = context;
        Dialog = new ProgressDialog(c);
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        return readJSONFeed(arg0[0]);
    }

    protected void onPreExecute() {
        Dialog.setMessage("Locating Breweries");

        Dialog.setTitle("Loading");
        Dialog.setCancelable(false);
        Dialog.show();
    }

    protected void onPostExecute(String result){
        //decode json here
        try{
            JSONObject json = new JSONObject(result);


            //acces listview
            ListView lv = (ListView) ((Activity) c).findViewById(R.id.locationList);

            //make array list for beer
            final List<BreweryLocationData> tasteList = new ArrayList<BreweryLocationData>();



            for(int i = 0; i < json.getJSONArray("data").length(); i++) {

                String brewery = json.getJSONArray("data").getJSONObject(i).getJSONObject("brewery").getString("name");
                String id = json.getJSONArray("data").getJSONObject(i).getJSONObject("brewery").getString("id");
                String latitude = json.getJSONArray("data").getJSONObject(i).getString("latitude");
                String longitude = json.getJSONArray("data").getJSONObject(i).getString("longitude");
                String distance = json.getJSONArray("data").getJSONObject(i).getString("distance");



                int count = i + 1;


                //create object
                BreweryLocationData tempLocation = new BreweryLocationData(brewery, id, longitude , latitude,distance);

                //add to arraylist
                tasteList.add(tempLocation);


                //add items to listview
                BreweryLocationInfoAdapter adapter1 = new BreweryLocationInfoAdapter(c ,R.layout.listview_item_row, tasteList);
                lv.setAdapter(adapter1);

                //set up clicks
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                                            int arg2, long arg3) {
                        BreweryLocationData o=(BreweryLocationData)arg0.getItemAtPosition(arg2);

                        String tempID = o.id;

                        Toast toast = Toast.makeText(c, tempID, Toast.LENGTH_SHORT);
                        toast.show();

                        //get beer details from id

                        Intent myIntent = new Intent(c, BreweryPage2.class);
                        myIntent.putExtra("id", tempID);
                        c.startActivity(myIntent);


                    }
                });




            }

        }
        catch(Exception e){

        }

        Dialog.dismiss();

    }

    public String readJSONFeed(String URL) {
        StringBuilder stringBuilder = new StringBuilder();
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream inputStream = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                inputStream.close();
            } else {
                Log.d("JSON", "Failed to download file");
            }
        } catch (Exception e) {
            Log.d("readJSONFeed", e.getLocalizedMessage());
        }
        return stringBuilder.toString();
    }

}
4

3 回答 3

1

尝试获取用户位置时,您正在强制关闭。我会添加验证用户是否启用了 GPS,如果没有,则给他们一个警告对话框,要求他们启用,您也可以将它们直接发送到 GPS 设置。

于 2013-09-01T12:49:33.053 回答
0

正如其他一些人指出的那样,它告诉您错误位于第 42 行。不幸的是,您稍微影响了将其粘贴到此处的行号。在您的第 42 行上,您假设某些内容不为空,而实际上它为空。

鉴于 LocationManager#getLastKnownLocation 可以返回 null,而您没有对此进行检查,我会说这是您的问题,并且您的第 42 行是您调用 location.getLongitude() 的地方。

如果您指定的提供者最近被使用过,getLastKnownLocation 将只返回一个位置。如果没有任何东西迫使它找到一个位置,或者很长时间(Android 认为最后一个位置太旧而不正确),那么你将得到 null。

于 2013-09-02T08:24:43.043 回答
0

用户可以重现错误吗?

从您的代码清单中,第 42 行似乎是(也许您已对其进行了编辑):

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

不明白为什么 getLatitude() 会中断而不是 getLongitude()

您应该验证位置,以确保您拥有它。

于 2013-09-01T15:00:14.910 回答