我想嘗試另一個全新的嘗試在android中從gps中檢索我的lat和lng。使用新代碼,它首先成功打開了我的活動,但我沒有獲得任何gps座標。然後,我拔掉了手機的電話並將其移向一扇窗戶,最終墜毀。然後我決定再試一次,但實際上插入了它,以便在窗口崩潰時得到錯誤報告。GPS崩潰活動
我開始應用第二次,我現在可以不再進入活動沒有它與此錯誤崩潰:
12-10 11:53:52.923 398-398/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main
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:2517)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5789)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.beerportfoliopro.FindBrewery.onLocationChanged(FindBrewery.java:67)
at com.example.beerportfoliopro.FindBrewery.onCreate(FindBrewery.java:42)
at android.app.Activity.performCreate(Activity.java:5195)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5789)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
at dalvik.system.NativeStart.main(Native Method)
12-10 11:53:52.933 24060-24091/system_process E/EmbeddedLogger﹕ App crashed! Process: com.beerportfolio.beerportfoliopro
12-10 11:53:52.933 24060-24091/system_process E/EmbeddedLogger﹕ App crashed! Package: com.beerportfolio.beerportfoliopro v22 (3.0)
12-10 11:53:52.933 24060-24091/system_process E/EmbeddedLogger﹕ Application Label: BeerPortfolio Pro
我活動的代碼是:
public class FindBrewery extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beer_location_list);
//latituteField = (TextView) findViewById(R.id.TextView02);
//longitudeField = (TextView) findViewById(R.id.TextView04);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
Toast.makeText(this, "Location Not available " + provider,
Toast.LENGTH_SHORT).show();
}
}
/* Request updates at startup */
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
Toast.makeText(this, "Attempting async task" + provider,
Toast.LENGTH_SHORT).show();
//call asycn task for location
String url = "myURL";
Log.d("urlTest", url);
//async task goes here
new GetNearbyBreweries(this).execute(url);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}
什麼我試圖完成的是,當活動開始時,它檢索用戶位置,然後將lng和lat發送到我的異步任務,然後從我的數據庫中檢索位置列表並填充列表。
忘記在調用'setText'方法之前初始化'latituteField'和'longitudeField' TextViews –
爲什麼要手動調用'onLocationChanged'?這是一個很大的不不否, – tyczj
onLocationChanged是一個回調。正如之前的評論所述,你不能直接調用它。 – NickT