提示时,Android应用程序不显示纬度和经度坐标

时间:2014-07-02 10:11:56

标签: java android google-maps gps

一个简单的应用程序,在textview中显示纬度和经度;由于某种原因,它不显示相关数据。香港专业教育学院提供了java代码,xml,manifesta和logcat。 代码

package com.example.trucktracker;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;

public class Locator extends ActionBarActivity {
     TextView textLat;
     TextView textLong;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_locator);
        textLat = (TextView)findViewById(R.id.textLat);
        textLong = (TextView)findViewById(R.id.textLong);
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new locatorlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
        if (savedInstanceState == null) 
        {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }
    class locatorlistener implements LocationListener
    {

        @Override
        public void onLocationChanged(Location location) {
            if(location != null)
            {
                double pLong = location.getLongitude();
                double pLat = location.getLatitude();
                textLat.setText(Double.toString(pLat));
                textLong.setText(Double.toString(pLong));
            }

        }

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

        }

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

        }

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Intent intent = getIntent();
        setContentView(R.layout.activity_locator);
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.locator, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_locator,
                    container, false);
            return rootView;
        }
    }

}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.trucktracker.Locator"
    tools:ignore="MergeRootFrame" >

    <TextView
        android:id="@+id/latitudetext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/latitude"
        tools:context=".Locator" />

    <TextView
        android:id="@+id/logitudetext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/latitudetext"
        android:text="@string/longitude" />

    <TextView
        android:id="@+id/textLat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/logitudetext"
        android:text=""
        android:layout_toRightOf="@+id/latitudetext" />

    <TextView
        android:id="@+id/textLong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/logitudetext"
        android:layout_alignBottom="@+id/logitudetext"
        android:text=""
        android:layout_toRightOf="@+id/logitudetext"/>

</RelativeLayout>

清单文件

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

logcat的

07-02 06:08:17.344: D/dalvikvm(535): GC_FOR_ALLOC freed 90K, 4% free 8346K/8648K, paused 62ms, total 69ms
07-02 06:08:17.464: I/Choreographer(535): Skipped 321 frames!  The application may be doing too much work on its main thread.
07-02 06:08:17.714: D/dalvikvm(535): GC_FOR_ALLOC freed 44K, 4% free 9914K/10224K, paused 63ms, total 64ms
07-02 06:08:17.884: I/Choreographer(535): Skipped 84 frames!  The application may be doing too much work on its main thread.
07-02 06:08:25.444: I/Choreographer(535): Skipped 41 frames!  The application may be doing too much work on its main thread.
07-02 06:08:25.614: I/Choreographer(535): Skipped 36 frames!  The application may be doing too much work on its main thread.
07-02 06:08:26.204: D/dalvikvm(535): GC_FOR_ALLOC freed 1404K, 15% free 9566K/11136K, paused 95ms, total 97ms
07-02 06:08:26.214: I/dalvikvm-heap(535): Grow heap (frag case) to 10.659MB for 1307824-byte allocation
07-02 06:08:26.284: D/dalvikvm(535): GC_FOR_ALLOC freed 2K, 13% free 10841K/12416K, paused 78ms, total 78ms
07-02 06:08:26.424: I/Choreographer(535): Skipped 81 frames!  The application may be doing too much work on its main thread.
07-02 06:08:27.854: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:27.854: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:27.854: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:27.864: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:27.884: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:27.894: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:27.894: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:27.894: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:27.914: I/ActivityManager(381): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.trucktracker/.MainActivity} from pid 535
07-02 06:08:27.964: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:28.014: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:28.014: E/SoundPool(381): error loading /system/media/audio/ui/KeypressStandard.ogg
07-02 06:08:28.014: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
07-02 06:08:28.014: E/SoundPool(381): error loading /system/media/audio/ui/KeypressSpacebar.ogg
07-02 06:08:28.024: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
07-02 06:08:28.024: E/SoundPool(381): error loading /system/media/audio/ui/KeypressDelete.ogg
07-02 06:08:28.024: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
07-02 06:08:28.024: E/SoundPool(381): error loading /system/media/audio/ui/KeypressReturn.ogg
07-02 06:08:28.024: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
07-02 06:08:28.024: E/SoundPool(381): error loading /system/media/audio/ui/KeypressInvalid.ogg
07-02 06:08:28.024: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
07-02 06:08:28.024: W/AudioService(381): onLoadSoundEffects(), Error -1 while loading samples
07-02 06:08:28.124: D/gralloc(49): Registering a buffer in the process that created it. This may cause memory ordering problems.
07-02 06:08:28.124: E/libEGL(49): called unimplemented OpenGL ES API
07-02 06:08:28.124: E/libEGL(49): called unimplemented OpenGL ES API
07-02 06:08:28.124: E/libEGL(49): called unimplemented OpenGL ES API
07-02 06:08:28.134: E/libEGL(49): called unimplemented OpenGL ES API
07-02 06:08:28.134: E/SurfaceFlinger(49): glCheckFramebufferStatusOES error -190605487
07-02 06:08:28.134: E/SurfaceFlinger(49): got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot
07-02 06:08:28.134: E/libEGL(49): called unimplemented OpenGL ES API
07-02 06:08:28.144: E/libEGL(49): called unimplemented OpenGL ES API
07-02 06:08:28.184: W/WindowManager(381): Screenshot failure taking screenshot for (246x410) to layer 21005
07-02 06:08:28.254: I/Choreographer(535): Skipped 97 frames!  The application may be doing too much work on its main thread.
07-02 06:08:28.774: I/ActivityManager(381): Start proc com.example.trucktracker for activity com.example.trucktracker/.MainActivity: pid=1104 uid=10052 gids={50052}
07-02 06:08:28.854: D/dalvikvm(1104): Not late-enabling CheckJNI (already on)
07-02 06:08:30.864: I/Choreographer(535): Skipped 42 frames!  The application may be doing too much work on its main thread.
07-02 06:08:31.134: I/Choreographer(381): Skipped 39 frames!  The application may be doing too much work on its main thread.
07-02 06:08:31.204: I/ARMAssembler(49): generated scanline__00000077:03010104_00008001_00000000 [ 89 ipp] (110 ins) at [0xb6199680:0xb6199838] in 10000000 ns
07-02 06:08:31.294: I/Choreographer(381): Skipped 40 frames!  The application may be doing too much work on its main thread.
07-02 06:08:32.064: D/dalvikvm(1104): GC_FOR_ALLOC freed 67K, 5% free 3135K/3276K, paused 50ms, total 54ms
07-02 06:08:32.064: I/dalvikvm-heap(1104): Grow heap (frag case) to 3.758MB for 659360-byte allocation
07-02 06:08:32.174: D/dalvikvm(1104): GC_FOR_ALLOC freed 6K, 4% free 3773K/3920K, paused 77ms, total 77ms
07-02 06:08:32.294: D/com.example.trucktracker.MainActivity(1104): Settings Text.
07-02 06:08:32.664: D/com.example.trucktracker.MainActivity(1104): Showing menu.
07-02 06:08:32.764: D/gralloc_goldfish(1104): Emulator without GPU emulation detected.
07-02 06:08:32.974: I/ActivityManager(381): Displayed com.example.trucktracker/.MainActivity: +4s304ms
07-02 06:08:36.234: D/com.example.trucktracker.MainActivity(1104): ClickedContinueButton
07-02 06:08:36.234: I/ActivityManager(381): START u0 {cmp=com.example.trucktracker/.Locator} from pid 1104
07-02 06:08:36.244: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:36.244: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:36.244: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:36.254: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:36.254: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:36.254: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:36.254: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:36.254: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:36.254: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:36.254: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-02 06:08:36.254: E/SoundPool(381): error loading /system/media/audio/ui/KeypressStandard.ogg
07-02 06:08:36.254: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
07-02 06:08:36.254: E/SoundPool(381): error loading /system/media/audio/ui/KeypressSpacebar.ogg
07-02 06:08:36.254: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
07-02 06:08:36.254: E/SoundPool(381): error loading /system/media/audio/ui/KeypressDelete.ogg
07-02 06:08:36.254: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
07-02 06:08:36.274: E/SoundPool(381): error loading /system/media/audio/ui/KeypressReturn.ogg
07-02 06:08:36.274: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
07-02 06:08:36.274: E/SoundPool(381): error loading /system/media/audio/ui/KeypressInvalid.ogg
07-02 06:08:36.284: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
07-02 06:08:36.324: W/AudioService(381): onLoadSoundEffects(), Error -1 while loading samples
07-02 06:08:36.484: I/Choreographer(1104): Skipped 33 frames!  The application may be doing too much work on its main thread.
07-02 06:08:38.174: I/Choreographer(381): Skipped 33 frames!  The application may be doing too much work on its main thread.
07-02 06:08:38.204: I/ActivityManager(381): Displayed com.example.trucktracker/.Locator: +1s804ms
07-02 06:08:38.474: I/Choreographer(381): Skipped 40 frames!  The application may be doing too much work on its main thread.
07-02 06:08:38.624: I/Choreographer(381): Skipped 36 frames!  The application may be doing too much work on its main thread.

2 个答案:

答案 0 :(得分:0)

首先要检查的是GPS启用了吗?

要检查的第二件事 - 您是否获得了位置锁定?您是否在通知栏上看到GPS符号?如果是这样闪烁或稳定?如果没有出现,GPS根本不会打开。如果它闪烁,你没有锁。在你有锁之前,它不会调用onLocationChanged。请注意,如果你在里面,你可能永远不会锁定。

您可以做的另一件事是将MOCK_LOCATIONS添加到您的权限并下载模拟位置应用。它会伪造GPS数据到您的应用程序进行测试。如果您这样做,请记住在发布之前删除该权限。

答案 1 :(得分:0)

请删除此

    Intent intent = getIntent();
    setContentView(R.layout.activity_locator);

来自

@Override
public boolean onCreateOptionsMenu(Menu menu)

每次打开菜单时,您基本上都在重新创建活动中的每个视图。这意味着您的textLat和textLong变量不再有效。您在其中设置的任何值都不会反​​映在应用