简体   繁体   中英

Android: Get Longitude and Latitude

I am trying to get the Longitude and Latitude displayed in the xml layout file but my app is always crashing. I have already craeted 4 TextView for the Latitude and Longitude in the xml file. I hope I will find help here

I appretierte any help.

Error:

03-29 23:34:06.376: E/AndroidRuntime(28201):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at android.os.Looper.loop(Looper.java:157)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at android.app.ActivityThread.main(ActivityThread.java:5356)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at java.lang.reflect.Method.invokeNative(Native Method)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at java.lang.reflect.Method.invoke(Method.java:515)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at dalvik.system.NativeStart.main(Native Method)
03-29 23:34:06.376: E/AndroidRuntime(28201): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.bustracker.GPSAPlanetActivity" on path: DexPathList[[zip file "/data/app/com.bustracker-3.apk"],nativeLibraryDirectories=[/data/app-lib/com.bustracker-3, /vendor/lib, /system/lib]]
03-29 23:34:06.376: E/AndroidRuntime(28201):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
03-29 23:34:06.376: E/AndroidRuntime(28201):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)
03-29 23:34:06.376: E/AndroidRuntime(28201):    ... 11 more

MainActivity:

package com.bustracker;

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    TextView textLat;
    TextView textLong;

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

        textLat = (TextView) findViewById(R.id.textLat);
        textLong = (TextView) findViewById(R.id.textLong);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new myLocationListener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

    }

    class myLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            if (location != null) {
                double plong = location.getLongitude();
                double pLat = location.getLatitude();

                textLat.setText(Double.toString(pLat));
                textLong.setText(Double.toString(plong));
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }
    }
}

Manifest:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".AdvancedSearchActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.AdvancedSearchActivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".PlantResultActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.PlantResultActivityy" />

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

</manifest>

Your manifest has the launcher icon tied to a GPSAPlanetActivity , and you do not have such an activity. Note that the Java code you posted is for a class named MainActivity .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM