0

私のアプリには、ユーザーの gps 座標を取得して近くの醸造所を返す機能があります。私がこの機能を使用すると、強制的に閉じられることはなく、他の人がテストして機能しました。ユーザーの場所を取得し、醸造所の場所を取得しようとするアクティビティを開いたときに、1 人のユーザーがこのエラーを報告しました。

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;
    }

}

最後に私の asynctask は次のとおりです。

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行目では、何かがnullではないと想定していますが、実際にはnullです。

LocationManager#getLastKnownLocation が null を返す可能性があり、それをチェックしていないことを考えると、それが問題であり、42 行目で location.getLongitude() を呼び出す場所であると言えます。

getLastKnownLocation は、指定したプロバイダーが最近使用された場合にのみ Location を返します。まだ場所を見つけることを強制していない場合、または長い間 (Android は最後の場所が古すぎて正しくないと見なします)、null を取得します。

于 2013-09-02T08:24:43.043 に答える