我嘗試爲我的研究創建一個跟蹤sportif的應用程序。Android:空指針例外GPS應用程序
當我發送一個新的定位,我的應用程序崩潰是由於這個錯誤:
03-18 18:44:34.662: E/AndroidRuntime(1085): FATAL EXCEPTION: main
03-18 18:44:34.662: E/AndroidRuntime(1085): java.lang.NullPointerException
03-18 18:44:34.662: E/AndroidRuntime(1085): at android.app.Activity.findViewById(Activity.java:1839)
03-18 18:44:34.662: E/AndroidRuntime(1085): at fr.polytech.trackersportif.GPSData.onLocationChanged(GPSData.java:28)
03-18 18:44:34.662: E/AndroidRuntime(1085): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:255)
03-18 18:44:34.662: E/AndroidRuntime(1085): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:184)
03-18 18:44:34.662: E/AndroidRuntime(1085): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:200)
03-18 18:44:34.662: E/AndroidRuntime(1085): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 18:44:34.662: E/AndroidRuntime(1085): at android.os.Looper.loop(Looper.java:137)
03-18 18:44:34.662: E/AndroidRuntime(1085): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-18 18:44:34.662: E/AndroidRuntime(1085): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 18:44:34.662: E/AndroidRuntime(1085): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 18:44:34.662: E/AndroidRuntime(1085): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-18 18:44:34.662: E/AndroidRuntime(1085): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-18 18:44:34.662: E/AndroidRuntime(1085): at dalvik.system.NativeStart.main(Native Method)
我mainActivity是:
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new GPSData();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, locationListener);
而要完成我的課(GPSData)是:
public class GPSData extends Activity implements LocationListener{
TextView textLong, textLat;
protected void onCreate(Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);
setContentView(R.layout.page_accueil); //page_acceuil is the layout where are the two textview that I want set text
}
@Override
public void onLocationChanged(Location location) {
textLong = (TextView) findViewById(R.id.textView1);
textLat = (TextView) findViewById(R.id.textView3);
textLong.setText(String.valueOf(location.getLongitude()));
textLat.setText(String.valueOf(location.getLatitude()));
}
因爲兩天我嘗試了不同的事情,但我一次又一次地出現這個錯誤。
你能幫助我嗎?
您要測試您的應用程序? – 2013-03-18 19:00:49
GPSData類不應擴展Activity。 GPSData應該是實現LocationListener的mainActivity中的私有類。 – BrianPlummer 2013-03-18 19:03:27