[Hilfe Benötigt] App stürzt ab

  • Antworten:5
Danny Hess
  • Forum-Beiträge: 17

08.12.2012, 02:33:28 via Website

Servus Comunity,

ich benötige eure hilfe, ich versuche mich gerade als neuling an einer Speed Hud App und komme nicht weiter.

Die App Läuft einwandfrei wenn ich folgenden Code nutze:
1import java.util.List;
2
3import android.app.Activity;
4import android.content.Context;
5import android.hardware.Sensor;
6import android.hardware.SensorEvent;
7import android.hardware.SensorEventListener;
8import android.hardware.SensorManager;
9import android.location.Criteria;
10import android.location.Location;
11import android.location.LocationListener;
12import android.location.LocationManager;
13import android.location.LocationProvider;
14import android.os.Bundle;
15import android.util.Log;
16import android.widget.TextView;
17import android.widget.Toast;
18
19public class Main extends Activity {
20
21 private static final String TAG = Main.class.getSimpleName();
22
23 TextView degree, tv_latitude, tv_longitude, tv_accuracy, speed;
24 private static SensorManager mySensorManager;
25 private boolean sersorrunning;
26 LocationManager manager;
27 LocationListener listener;
28
29 @Override
30 protected void onCreate(Bundle savedInstanceState) {
31 // TODO Auto-generated method stub
32 super.onCreate(savedInstanceState);
33 setContentView(R.layout.main);
34
35 degree = (TextView) findViewById(R.id.degree);
36 tv_latitude = (TextView) findViewById(R.id.tv_latitude);
37 tv_longitude = (TextView) findViewById(R.id.tv_longitude);
38 tv_accuracy = (TextView) findViewById(R.id.tv_accuracy);
39
40 mySensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
41 @SuppressWarnings("deprecation")
42 List<Sensor> mySensors = mySensorManager
43 .getSensorList(Sensor.TYPE_ORIENTATION);
44
45 if (mySensors.size() > 0) {
46 mySensorManager.registerListener(mySensorEventListener,
47 mySensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
48 sersorrunning = true;
49 Toast.makeText(this, "Start ORIENTATION Sensor", Toast.LENGTH_LONG)
50 .show();
51 } else {
52 Toast.makeText(this, "No ORIENTATION Sensor", Toast.LENGTH_LONG)
53 .show();
54 sersorrunning = false;
55 finish();
56 }
57
58 manager = (LocationManager) getSystemService(LOCATION_SERVICE);
59
60 List<String> providers = manager.getAllProviders();
61 for (String name : providers) {
62 LocationProvider lp = manager.getProvider(name);
63 Log.d(TAG,
64 lp.getName() + " --- isProviderEnabled(): "
65 + manager.isProviderEnabled(name));
66 Log.d(TAG, "requiresCell(): " + lp.requiresCell());
67 Log.d(TAG, "requiresNetwork(): " + lp.requiresNetwork());
68 Log.d(TAG, "requiresSatellite(): " + lp.requiresSatellite());
69
70 }
71 Criteria criteria = new Criteria();
72 criteria.setAccuracy(Criteria.ACCURACY_FINE);
73 criteria.setPowerRequirement(Criteria.POWER_HIGH);
74 String name = manager.getBestProvider(criteria, true);
75 Log.d(TAG, name);
76 listener = new LocationListener() {
77
78 public void onLocationChanged(Location location) {
79 // TODO Auto-generated method stub
80 Log.d(TAG, "onLocationChanged()");
81 if (location != null) {
82 String lat = Location.convert(location.getLatitude(),
83 Location.FORMAT_SECONDS);
84 tv_latitude.setText(lat);
85 String lon = Location.convert(location.getLongitude(),
86 Location.FORMAT_SECONDS);
87 tv_longitude.setText(lon);
88 String acc = "" + location.getAccuracy();
89 tv_accuracy.setText(String.format("%2.0f m",
90 (location.getAccuracy())));
91 }
92 }
93
94 public void onProviderDisabled(String provider) {
95 // TODO Auto-generated method stub
96 Log.d(TAG, "onProviderDisabled()");
97 }
98
99 public void onProviderEnabled(String provider) {
100 // TODO Auto-generated method stub
101 Log.d(TAG, "onProviderEnabled()");
102 }
103
104 public void onStatusChanged(String provider, int status,
105 Bundle extras) {
106 // TODO Auto-generated method stub
107 Log.d(TAG, "onStatusChanged()");
108 }
109
110 };
111
112 }
113
114 @Override
115 protected void onStart() {
116 super.onStart();
117 Log.d(TAG, "onStart()");
118 manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
119 listener);
120 }
121
122 @Override
123 protected void onPause() {
124 super.onPause();
125 Log.d(TAG, "onPause()");
126 manager.removeUpdates(listener);
127 }
128
129 private SensorEventListener mySensorEventListener = new SensorEventListener() {
130
131 public void onSensorChanged(SensorEvent event) {
132 // TODO Auto-generated method stub
133
134 degree.setText(String.format("%03.0f°", event.values[0]));
135
136 }
137
138 public void onAccuracyChanged(Sensor sensor, int accuracy) {
139 // TODO Auto-generated method stub
140
141 }
142 };
143
144 @Override
145 protected void onDestroy() {
146 // TODO Auto-generated method stub
147 super.onDestroy();
148 if (sersorrunning) {
149 mySensorManager.unregisterListener(mySensorEventListener);
150
151 }
152 }
153
154}


sobald ich aber:
1public void onLocationChanged(Location location) {
2...
3speed.setText(String.format("%2.0f",(location.getSpeed())));
4}

eingebe, kommt "Leider Wurde die App beendet"

Ich habe in der Manifest.xml
1<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
verwendet.

Würde mich über eure hilfe freuen.

Gruße

PS: Hier die LogCat:
112-08 02:29:57.348: D/Main(3112): onLocationChanged()
212-08 02:29:57.348: D/AndroidRuntime(3112): Shutting down VM
312-08 02:29:57.348: W/dalvikvm(3112): threadid=1: thread exiting with uncaught exception (group=0x40aa4a08)
412-08 02:29:57.348: E/AndroidRuntime(3112): FATAL EXCEPTION: main
512-08 02:29:57.348: E/AndroidRuntime(3112): java.lang.NullPointerException
612-08 02:29:57.348: E/AndroidRuntime(3112): at de.madhess.speedhud.Main$2.onLocationChanged(Main.java:93)
712-08 02:29:57.348: E/AndroidRuntime(3112): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:234)
812-08 02:29:57.348: E/AndroidRuntime(3112): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:167)
912-08 02:29:57.348: E/AndroidRuntime(3112): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:183)
1012-08 02:29:57.348: E/AndroidRuntime(3112): at android.os.Handler.dispatchMessage(Handler.java:99)
1112-08 02:29:57.348: E/AndroidRuntime(3112): at android.os.Looper.loop(Looper.java:156)
1212-08 02:29:57.348: E/AndroidRuntime(3112): at android.app.ActivityThread.main(ActivityThread.java:5045)
1312-08 02:29:57.348: E/AndroidRuntime(3112): at java.lang.reflect.Method.invokeNative(Native Method)
1412-08 02:29:57.348: E/AndroidRuntime(3112): at java.lang.reflect.Method.invoke(Method.java:511)
1512-08 02:29:57.348: E/AndroidRuntime(3112): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
1612-08 02:29:57.348: E/AndroidRuntime(3112): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
1712-08 02:29:57.348: E/AndroidRuntime(3112): at dalvik.system.NativeStart.main(Native Method)
1812-08 02:30:01.202: D/Process(3112): killProcess, pid=3112
1912-08 02:30:01.202: D/Process(3112): dalvik.system.VMStack.getThreadStackTrace(Native Method)

Antworten
Klaus T.
  • Forum-Beiträge: 8.183

08.12.2012, 07:49:13 via Website

Danny Hess
112-08 02:29:57.348: E/AndroidRuntime(3112): java.lang.NullPointerException
212-08 02:29:57.348: E/AndroidRuntime(3112): at de.madhess.speedhud.Main$2.onLocationChanged(Main.java:93)

Da steht doch, WO und WARUM! Dann überprüft doch einfach deinen Code an der Stelle und/oder trace und guck nach :rolleyes:

if all else fails, read the instructions.

Antworten
San Blarnoi
  • Forum-Beiträge: 2.545

08.12.2012, 11:07:32 via Website

Vielleicht würde es helfen, die Variable speed auch mal zu initialisieren?

Antworten
Danny Hess
  • Forum-Beiträge: 17

09.12.2012, 20:01:27 via App

and dev
Vielleicht würde es helfen, die Variable speed auch mal zu initialisieren?

Danke für den Hinweis, habe speed initialisiert und schon geht die App. Manchmal sieht man den Wald vor lauter Bäumen nicht :-D

Antworten
San Blarnoi
  • Forum-Beiträge: 2.545

09.12.2012, 23:34:44 via Website

Gerne.

Als Tipp fürs nächste mal: logcat führt dich genau auf die Zeile, in der das Problem aufgetreten ist;
wenn du das Problem mit "Hingucken" nicht findest, setze einen Breakpoint an die Stelle, starte die App im Debugger und schau dann die Variablen in der Zeile an, wenn der Debugger die Ausführung stoppt.

Antworten
Danny Hess
  • Forum-Beiträge: 17

10.12.2012, 02:29:57 via App

Danke, werde ich das nächste mal beachten.

Antworten