GPS Daten

T

TimoFrenzel

Neues Mitglied
0
Hi folks,

ja ich bin neu hier^^

Und gleich eine für mich harte Nuss.
Vielleicht kann mir hier einer helfen.

Ich bin gerade dabei eine mehr als einfach App zu bauen.

Ein Button, klickt man drauf, soll die App die Standortdaten und ein paar weitere Daten an eine Webseite senden.

Leider habe ich nun fast alle Tuts und Hinweise aus anderen Foren durch.
Das Ergebnis ist immer das selbe... Die App stürzt ab, bzw wird einfach beendet.


Über ein oder einpaar helfende Worte oder mehr würde ich mich freuen.
Es kann doch nicht so schwer sein....


Anbei die Info....
es crashd auf den Handys wie auch in meiner Eclipse IDE.

Ich nutze hier Android 4.2.2

Hier meine MainActivity.java

Code:
package de.xxx.xxxbrokenstream;

import de.xxx.xxx.helper.GPSTracker;
import de.xxx.xxxbrokenstream.R;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public abstract class MainActivity extends Activity implements LocationListener {

    double lng;
    double lat;

    Location location;
    private String latitudeTmp;
    private String longitudeTmp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);

        //this.setDefaultButton();
        this.getLocation();
        
        latitudeTmp = String.valueOf(lat);
        longitudeTmp = String.valueOf(lng);
        
        
        System.out.println(latitudeTmp + " / " + longitudeTmp);
        
    };
    
    public void getLocation() {
        GPSTracker gps = new GPSTracker(this);
        if(gps.canGetLocation()){ 
            lat = gps.getLatitude(); // returns latitude
            lng = gps.getLongitude(); // returns longitude
        }
        if(!gps.canGetLocation())
        {
            gps.showSettingsAlert();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    };

    public void setDefaultButton() {
        Button button1 = (Button) findViewById(R.id.btn_1);
        button1.setVisibility(View.VISIBLE);
        button1.setOnClickListener(1Listener);

        Button button2 = (Button) findViewById(R.id.btn_2);
        button2.setVisibility(View.VISIBLE);
        button2.setOnClickListener(2Listener);

        Button button3 = (Button) findViewById(R.id.btn_3);
        button3.setVisibility(View.VISIBLE);
        button3.setOnClickListener(3Listener);

    };

    // Listener

    private OnClickListener 1Listener = new OnClickListener() {
        public void onClick(View v) {
            // do something when the button is clicked

            //System.out.println(currentLat + " / " + currentLong);

        }

    };

    private OnClickListener 2Listener = new OnClickListener() {
        public void onClick(View v) {
            // do something when the button is clicked

            //System.out.println(currentLat + " / " + currentLong);

        }
    };

    private OnClickListener 3Listener = new OnClickListener() {
        public void onClick(View v) {
            // do something when the button is clicked

            //System.out.println(currentLat + " / " + currentLong);
        }
    };

}
Meine GPSTracker.java

Code:
package de.xxx.xxx.helper;

import de.xxx.xxxbrokenstream.R;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;

public class GPSTracker extends Service implements LocationListener {

private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}
public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);
        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}
@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}
public double getLatitude(){
    if(location != null){
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
    }
    return longitude;
}
public boolean canGetLocation() {
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog
 * */
public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS is settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // Setting Icon to Dialog
    alertDialog.setIcon(R.drawable.ic_launcher);

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}
public void stopUsingGPS(){
    if(locationManager != null){
        locationManager.removeUpdates(GPSTracker.this);
    }      
}
}
Die bisherigen Permissions:

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


Die Fehlermeldungen aus dem Log:

09-13 10:28:51.859: E/AndroidRuntime(783): FATAL EXCEPTION: main
09-13 10:28:51.859: E/AndroidRuntime(783): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{de.xxx.xxxbrokenstream/de.xxx.xxxbrokenstream.MainActivity}: java.lang.InstantiationException: can't instantiate class de.xxx.xxxbrokenstream.MainActivity
09-13 10:28:51.859: E/AndroidRuntime(783): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
09-13 10:28:51.859: E/AndroidRuntime(783): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-13 10:28:51.859: E/AndroidRuntime(783): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-13 10:28:51.859: E/AndroidRuntime(783): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-13 10:28:51.859: E/AndroidRuntime(783): at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 10:28:51.859: E/AndroidRuntime(783): at android.os.Looper.loop(Looper.java:137)
09-13 10:28:51.859: E/AndroidRuntime(783): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-13 10:28:51.859: E/AndroidRuntime(783): at java.lang.reflect.Method.invokeNative(Native Method)
09-13 10:28:51.859: E/AndroidRuntime(783): at java.lang.reflect.Method.invoke(Method.java:511)
09-13 10:28:51.859: E/AndroidRuntime(783): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-13 10:28:51.859: E/AndroidRuntime(783): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-13 10:28:51.859: E/AndroidRuntime(783): at dalvik.system.NativeStart.main(Native Method)
09-13 10:28:51.859: E/AndroidRuntime(783): Caused by: java.lang.InstantiationException: can't instantiate class de.xxx.xxxbrokenstream.MainActivity
09-13 10:28:51.859: E/AndroidRuntime(783): at java.lang.Class.newInstanceImpl(Native Method)
09-13 10:28:51.859: E/AndroidRuntime(783): at java.lang.Class.newInstance(Class.java:1319)
09-13 10:28:51.859: E/AndroidRuntime(783): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
09-13 10:28:51.859: E/AndroidRuntime(783): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
09-13 10:28:51.859: E/AndroidRuntime(783): ... 11 more
09-13 10:28:51.879: W/ActivityManager(276): Force finishing activity de.xxx.xxxbrokenstream/.MainActivity

 
Zuletzt bearbeitet:
Hi,

hast Du deine Activity in der Manifest deklariert?
 
Max1809 schrieb:
Hi,

hast Du deine Activity in der Manifest deklariert?



Hi,
ja habe ich.
Hier das ganze Manifest.


HTML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.xxx.xxxbrokenstream"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="de.xxx.xxxbrokenstream.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
 
Sehe ich richtig, dass der GPSTracker bei Dir in einem anderen Package liegt? Hast Du das Package im Build Path hinterlegt?

Versuch mal Rechtslick auf das Projekt->Android Tools->Fix Project Properties (in Eclipse)
 
Ich bin mir nicht sicher, aber ich halte es für keine gute Idee die GPS Daten schon in der onCreate Methode aufzurufen.
In der Methode ist deine Activity einfach noch nicht ganz fertig.
Ich würde das eher in der OnStart Methode machen.

Zumal du ja sogar evtl ein Dialog zeigst, das geht aber erst nach OnCreate.
Activity | Android Developers
 
Yuck schrieb:
Android GPS Tutorial - ev. hilft dir das tutorial weiter


danke, schau ich mir gleich an.

Der ursprüngliche Beitrag von 13:27 Uhr wurde um 13:28 Uhr ergänzt:

Max1809 schrieb:
Sehe ich richtig, dass der GPSTracker bei Dir in einem anderen Package liegt? Hast Du das Package im Build Path hinterlegt?

Versuch mal Rechtslick auf das Projekt->Android Tools->Fix Project Properties (in Eclipse)


hat nicht gebraucht.
aber ja, hatte das package hier falsch notiert.

Der ursprüngliche Beitrag von 13:28 Uhr wurde um 14:08 Uhr ergänzt:

amfa schrieb:
Ich bin mir nicht sicher, aber ich halte es für keine gute Idee die GPS Daten schon in der onCreate Methode aufzurufen.
In der Methode ist deine Activity einfach noch nicht ganz fertig.
Ich würde das eher in der OnStart Methode machen.

Zumal du ja sogar evtl ein Dialog zeigst, das geht aber erst nach OnCreate.
Activity | Android Developers



Berechtigter Einwand...

habe das nun so...

Code:
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        this.setDefaultButton();
        
    };
    
    protected void onStart(){
        this.getLocation();
        
        latitudeTmp = String.valueOf(lat);
        longitudeTmp = String.valueOf(lng);
        
        System.out.println(latitudeTmp + " / " + longitudeTmp);
    }


ich werd mal weiter debuggen und meld mich wieder.

Der ursprüngliche Beitrag von 14:08 Uhr wurde um 15:02 Uhr ergänzt:

dank des Tutorials läuft es... danke
 

Ähnliche Themen

A
Antworten
10
Aufrufe
1.021
swa00
swa00
A
Antworten
10
Aufrufe
1.925
swa00
swa00
M
  • MikelKatzengreis
Antworten
10
Aufrufe
223
swa00
swa00
Zurück
Oben Unten