2013-09-25 97 views
-2
根據書中的教程

提出申請,但有一些錯誤,在不爲人知的地方墜毀我的程序:logcat的不給錯誤信息 所以我的位置class`代碼:崩潰的位置程序

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    private LocationManager myLocationManager; 
    private LocationListener myLocationListener; 
    private TextView myLatitude, myLongitude; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 

     myLatitude = (TextView)findViewById(R.id.textView1); 
     myLongitude = (TextView)findViewById(R.id.textView2); 



     myLocationManager = (LocationManager)getSystemService(
      Context.LOCATION_SERVICE); 

     myLocationListener = new MyLocationListener(); 

     myLocationManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, 
       0, 
       0, 
       myLocationListener); 

     //Get the current location in start-up 
     myLatitude.setText(String.valueOf(
      myLocationManager.getLastKnownLocation(
       LocationManager.GPS_PROVIDER).getLatitude())); 

     myLongitude.setText(String.valueOf(
      myLocationManager.getLastKnownLocation(
       LocationManager.GPS_PROVIDER).getLongitude())); 
    } 

    private class MyLocationListener implements LocationListener{ 

     public void onLocationChanged(Location argLocation) { 
     // TODO Auto-generated method stub 
     myLatitude.setText(String.valueOf(
      argLocation.getLatitude())); 
     myLongitude.setText(String.valueOf(
      argLocation.getLongitude())); 
     } 

     public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 
     } 

     public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 
     } 

     public void onStatusChanged(String provider, 
     int status, Bundle extras) { 
     // TODO Auto-generated method stub 
     } 
     }; 
} 

我旁邊添加權限清單文件: 在我的Galaxy Tab中啓動應用程序,我有碰撞,但(如果幫助)之後,從debuger在工具欄圖標飛濺「GPS」 代碼幾秒鐘後

DalvikVM[localhost:8624]  
    Thread [<1> main] (Suspended (exception RuntimeException)) 
     <VM does not provide monitor information> 
     ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2663 
     ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679 
     ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125 
     ActivityThread$H.handleMessage(Message) line: 2033 
     ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
     Looper.loop() line: 123 
     ActivityThread.main(String[]) line: 4627  
     Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] 
     Method.invoke(Object, Object...) line: 521 
     ZygoteInit$MethodAndArgsCaller.run() line: 868 
     ZygoteInit.main(String[]) line: 626 
     NativeStart.main(String[]) line: not available [native method] 
    Thread [<6> Binder Thread #2] (Running) 
    Thread [<5> Binder Thread #1] (Running) 
+1

安置自己的錯誤代碼。 –

回答

0

正是這些線路:

//Get the current location in start-up 
myLatitude.setText(String.valueOf(
myLocationManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER).getLatitude())); 

導致空指針

最後已知的位置將是空的,直到onLocationChanged()已經在這裏觸發

+0

但是當我清理這個代碼崩潰不停止=( 請告訴我這種情況下正常工作的代碼 –