Bonjour,

J'essaie de faire une application où je dois juste afficher la longitude et la latitude de mon mobile en utilisant le GPS. Voici le stack trace que je reçois :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
04-16 17:03:09.526: WARN/dalvikvm(1263): threadid=3: thread exiting with uncaught exception (group=0x4001e390)
04-16 17:03:09.526: ERROR/AndroidRuntime(1263): Uncaught handler: thread main exiting due to uncaught exception
04-16 17:03:09.536: ERROR/AndroidRuntime(1263): java.lang.NullPointerException
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at android.network.Android.onClick(Android.java:69)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at android.view.View.performClick(View.java:2364)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at android.view.View.onTouchEvent(View.java:4179)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at android.widget.TextView.onTouchEvent(TextView.java:6613)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at android.view.View.dispatchTouchEvent(View.java:3709)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at android.app.Activity.dispatchTouchEvent(Activity.java:2068)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1697)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at android.os.Looper.loop(Looper.java:123)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at android.app.ActivityThread.main(ActivityThread.java:4595)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at java.lang.reflect.Method.invokeNative(Native Method)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at java.lang.reflect.Method.invoke(Method.java:521)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-16 17:03:09.536: ERROR/AndroidRuntime(1263):     at dalvik.system.NativeStart.main(Native Method)
Le code GPS :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 
 
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
 
public class Gps implements LocationListener {
	private Location location;
	private Criteria criteria;
	private LocationManager locationManager;
 
	/**
         * constructeur de la classe GPS
         * 
         * @param locationManager
         *            gestionnaire de localisation
         * 
         */
 
	public Gps(LocationManager locationManager) {
		this.criteria = new Criteria();
		this.criteria.setAccuracy(Criteria.ACCURACY_FINE);
		this.criteria.setAltitudeRequired(false);
		this.criteria.setBearingRequired(false);
		this.criteria.setCostAllowed(true);
		this.criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
		this.locationManager = locationManager;
		this.locationManager.requestLocationUpdates(locationManager
				.getBestProvider(criteria, true), 100, 1, this);
	}
 
	public Location getLocation(){
		return this.location;
	}
 
	@Override
	public void onLocationChanged(Location loc) {
		// TODO Auto-generated method stub
		this.location = loc;
	}
 
	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
 
	}
 
}
l'activité

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 
 
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Date;
 
//...
 
public class Android extends Activity implements View.OnClickListener {
	private Button btn;
	private TextView txt;
	private LinearLayout layout;
	private Gps gps;
 
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		this.layout = new LinearLayout(this);
 
		btn = new Button(this);
		btn.setText("What time is it ! ");
		btn.setOnClickListener(this);
		txt = new TextView(this);
 
		layout.addView(btn);
		layout.addView(txt);
		layout.setPadding(50, 50, 0, 0);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setGravity(Gravity.CENTER);
		setContentView(layout);
		this.gps = new Gps((LocationManager)getSystemService(Context.LOCATION_SERVICE));
 
	}
//...
	public void onClick(View view) {
		if (view == btn) {
			Log.e("GPS", "long = " + gps.getLocation().getLongitude());
			Log.e("GPS", "lat = " + gps.getLocation().getLatitude());
		} else {
			// kill 
		}
 
	}
 
}
J'ai ajouté au manifest la permission "android.permission.ACCESS_FINE_LOCATION"