我的应用中有一项功能可以获取用户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;
}
}
最后我的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();
}
}
答案 0 :(得分:1)
在尝试获取用户位置时,您正在关闭力量。我会添加验证,表明该用途已启用GPS,如果没有,则会给他们一个警告对话框,要求他们启用,您也可以将它们直接发送到GPS设置。
答案 1 :(得分: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()
您应该验证位置,以确保您拥有它。
答案 2 :(得分:0)
正如其他几个人所指出的那样,它告诉你错误是在第42行。不幸的是,你在这里粘贴它的线号略有影响。在你的第42行,你假设某些东西不是空的,它实际上是空的。
鉴于LocationManager#getLastKnownLocation可以返回null,而你没有检查它,我会说这是你的问题,你的第42行是你调用location.getLongitude()的地方。
如果最近使用了您指定的提供程序,则getLastKnownLocation将仅返回位置。如果没有任何东西迫使它找到一个位置,或者很长一段时间(Android认为最后一个位置太旧而不正确),那么你将得到null。