2012-04-07 83 views
0

我只是想在地圖上顯示一個用戶的位置,我是很新的Android開發,但我在Java基礎。發送位置,設備崩潰的應用程序(機器人)

基本上我在Eclipse(郎/長協ORDS)儘快發送模擬數據,該數據被髮送我的應用程序崩潰,並顯示以下內容:

04-07 21:07:52.252: E/AndroidRuntime(639): FATAL EXCEPTION: main 
04-07 21:07:52.252: E/AndroidRuntime(639): java.lang.NullPointerException 
04-07 21:07:52.252: E/AndroidRuntime(639): at com.herghost.bmad.BMAD$1.onLocationChanged(BMAD.java:60) 
04-07 21:07:52.252: E/AndroidRuntime(639): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227) 
04-07 21:07:52.252: E/AndroidRuntime(639): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160) 
04-07 21:07:52.252: E/AndroidRuntime(639): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176) 
04-07 21:07:52.252: E/AndroidRuntime(639): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-07 21:07:52.252: E/AndroidRuntime(639): at android.os.Looper.loop(Looper.java:137) 
04-07 21:07:52.252: E/AndroidRuntime(639): at android.app.ActivityThread.main(ActivityThread.java:4424) 
04-07 21:07:52.252: E/AndroidRuntime(639): at java.lang.reflect.Method.invokeNative(Native Method) 
04-07 21:07:52.252: E/AndroidRuntime(639): at java.lang.reflect.Method.invoke(Method.java:511) 
04-07 21:07:52.252: E/AndroidRuntime(639): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
04-07 21:07:52.252: E/AndroidRuntime(639): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
04-07 21:07:52.252: E/AndroidRuntime(639): at dalvik.system.NativeStart.main(Native Method) 

我的應用程序的代碼是:

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class BMAD extends Activity { 


    private LocationManager locmgr = null; 
    private TextView mytext; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 



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

     final Button button = (Button) 
     findViewById(R.id.launchmap); 
     button.setOnClickListener(new Button.OnClickListener() 
     { 
      public void onClick(View view) 
       { 
        try 
         { 
          LocationManager address = locmgr; 
          Intent geoIntent = new Intent 
          (android.content.Intent.ACTION_VIEW, 
          Uri.parse("geo:0,0?q=" + address)); 
          startActivity(geoIntent); 
         } 
        catch (Exception e) 
         { 

         } 
       } 


     });} 
//Start a location listener 
LocationListener onLocationChange=new LocationListener() { 
    public void onLocationChanged(Location loc) { 
     //sets and displays the lat/long when a location is provided 
     String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude(); 
     mytext.setText(latlong); 
    } 

    public void onProviderDisabled(String provider) { 
    // required for interface, not used 
    } 

    public void onProviderEnabled(String provider) { 
    // required for interface, not used 
    } 

    public void onStatusChanged(String provider, int status, 
    Bundle extras) { 
    // required for interface, not used 
    } 
}; 

//pauses listener while app is inactive 
@Override 
public void onPause() { 
    super.onPause(); 
    locmgr.removeUpdates(onLocationChange); 
} 

//reactivates listener when app is resumed 
@Override 
public void onResume() { 
    super.onResume(); 
    locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,10000.0f,onLocationChange); 
} 
} 

任何人都可以讓我知道是什麼原因造成的?

回答

0

你不初始化變量mytext的。它會導致您的代碼爲mytext.setText(latlong);的第60行的NPE。

你應該這樣做mytext = findViewById(R.id.mytext);onCreate()方法。

+0

這就是它!由於 – dave 2012-04-08 19:29:30

0

嗯....你在UI需要所謂的MapView對象.... mapView = (MapView) findViewById(R.id.mapview); 但對於使用GeoPossition,這是更容易的,你可以使用指南針太:)

/**************** onCreate *******************/ 

private MyLocationOverlay myLocationOverlay = null; 

myLocationOverlay = new MyLocationOverlay(this, mapView); 
try { 
    myLocationOverlay.enableCompass(); 
    myLocationOverlay.enableMyLocation(); 
} catch (Exception e) { 
    // TODO: handle exception 
    Log.e("Activar brujula, localizacion LocationOverlay", e.getMessage()); 
} 
mapView.getOverlays().add(myLocationOverlay); 

/********************** onCreate end ***********************/ 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    // activamos la brujula y la localizacion 
    myLocationOverlay.enableMyLocation(); 
    myLocationOverlay.enableCompass(); 
} 

@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    // desactivamos la brujula y la localizacion 
    myLocationOverlay.disableMyLocation(); 
    myLocationOverlay.disableCompass(); 
} 
/*//////////////////////////////////////////////////////////////////////////*/ 

喝彩! ,

爲例) || / exemple MapView Project

+0

5秒差;) – 2012-04-07 21:27:55

+0

此非常感謝,我已經收藏它作爲當我到達我的應用程序的一部分參考。 – dave 2012-04-08 19:29:13

相關問題