2013-12-10 165 views
-2

我想嘗試另一個全新的嘗試在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發送到我的異步任務,然後從我的數據庫中檢索位置列表並填充列表。

+3

忘記在調用'setText'方法之前初始化'latituteField'和'longitudeField' TextViews –

+2

爲什麼要手動調用'onLocationChanged'?這是一個很大的不不否, – tyczj

+0

onLocationChanged是一個回調。正如之前的評論所述,你不能直接調用它。 – NickT

回答

3

你的問題是在這裏:

 Location location = locationManager.getLastKnownLocation(provider); 

    // Initialize the location fields 
    if (location != null) { 
     System.out.println("Provider " + provider + " has been selected."); 
     onLocationChanged(location); 

你叫onLocationChanged編程。這是錯誤的。你真正想要的是locationManager.requestLocationUpdates。在這裏看到:

http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String,%20long,%20float,%20android.location.LocationListener)

您所在的位置爲NULL,因爲你告訴你的應用程序,你有一個位置的時候,其實你不知道。當您這樣做時,會導致NullPointerException:

int lat = (int) (location.getLatitude()); 
    int lng = (int) (location.getLongitude()); 
    latituteField.setText(String.valueOf(lat)); 
    longitudeField.setText(String.valueOf(lng)); 

如果您等待位置更新觸發,則它將正常工作。

+0

真正的問題是與兩個無人值守的TextViews latituteField和longitudeField ...位置不爲空,因爲他實際上檢查locationManager.getLastKnownLocation(provider)後的空位置; – luciofm

+0

@luciofm這也是一個問題。我沒有看到'latituteField'和'longitudeField'在他的示例代碼中的任何地方被初始化。即使他們是,他從來沒有要求LocationManager獲取位置。 –

+0

他詢問最後一個已知位置locationManager.getLastKnownLocation(provider); – luciofm

0

或者您可能沒有添加獲取gps位置所需的權限,它可能會給您NullPointerException,因爲它是空的。