为什么不显示纬度和经度

时间:2016-01-11 14:31:04

标签: android dictionary android-maps-v2

我在代码中找不到任何错误,但是没有显示纬度和经度。我无法理解为什么它不显示&这是否是android sdk 23的问题。我输出为空白屏幕。

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.widget.TextView;

import android.util.Log;

public class MainActivity extends Activity implements LocationListener{
    protected LocationManager locationManager;
    protected LocationListener locationListener;
    protected Context context;
    TextView txtLat;
    String lat;
    String provider;
    protected String latitude,longitude;
    protected boolean gps_enabled,network_enabled;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtLat = (TextView) findViewById(R.id.textview1);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
                {

            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        }


    }
    @Override
    public void onLocationChanged(Location location) {
        txtLat = (TextView) findViewById(R.id.textview1);
        txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("Latitude","disable");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("Latitude","enable");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Latitude","status");
    }
}

xml文件

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
</RelativeLayout>

清单文件

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

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

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

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

</manifest>

日志文件

01-11 20:41:35.424 27160-27160/? I/art: Late-enabling -Xcheck:jni
01-11 20:41:35.774 27160-27199/com.binox.myapplication D/OpenGLRenderer: Render dirty regions requested: true
01-11 20:41:35.784 27160-27160/com.binox.myapplication D/Atlas: Validating map...
01-11 20:41:35.864 27160-27199/com.binox.myapplication I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.017_msm8916_64_LA.BR.1.1.3_RB1__release_AU (Ie228694f41)
01-11 20:41:35.864 27160-27199/com.binox.myapplication I/Adreno-EGL: OpenGL ES Shader Compiler Version: E031.25.03.04
01-11 20:41:35.864 27160-27199/com.binox.myapplication I/Adreno-EGL: Build Date: 09/29/15 Tue
01-11 20:41:35.864 27160-27199/com.binox.myapplication I/Adreno-EGL: Local Branch: mybranch14662643
01-11 20:41:35.864 27160-27199/com.binox.myapplication I/Adreno-EGL: Remote Branch: quic/LA.BR.1.1.3_rb1.32
01-11 20:41:35.864 27160-27199/com.binox.myapplication I/Adreno-EGL: Local Patches: NONE
01-11 20:41:35.864 27160-27199/com.binox.myapplication I/Adreno-EGL: Reconstruct Branch: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.017 + 26a3cba + 6f69ea6 + 8bc2bc8 + 649fcde + a52cccf + dbf281f + 15f0bf8 + 8d02f76 + 9b2cb1a + 25f3b04 + 7cd8c84 + b54906e + 675fd74 + 7c22ef4 + 79b094c 
01-11 20:41:35.864 27160-27199/com.binox.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
01-11 20:41:35.884 27160-27199/com.binox.myapplication D/OpenGLRenderer: Enabling debug mode 0
01-11 20:41:35.884 27160-27199/com.binox.myapplication I/qdutils: PartialUpdate status: Disabled
01-11 20:41:35.884 27160-27199/com.binox.myapplication I/qdutils: Left Align: 0
01-11 20:41:35.884 27160-27199/com.binox.myapplication I/qdutils: Width Align: 0
01-11 20:41:35.884 27160-27199/com.binox.myapplication I/qdutils: Top Align: 0
01-11 20:41:35.884 27160-27199/com.binox.myapplication I/qdutils: Height Align: 0
01-11 20:41:35.884 27160-27199/com.binox.myapplication I/qdutils: Min ROI Width: 0
01-11 20:41:35.884 27160-27199/com.binox.myapplication I/qdutils: Min ROI Height: 0
01-11 20:41:35.884 27160-27199/com.binox.myapplication I/qdutils: Needs ROI Merge: 0
01-11 20:41:35.884 27160-27199/com.binox.myapplication I/qdutils: Dynamic Fps: Enabled
01-11 20:41:35.884 27160-27199/com.binox.myapplication I/qdutils: Min Panel fps: 45
01-11 20:41:35.884 27160-27199/com.binox.myapplication I/qdutils: Max Panel fps: 60
01-11 20:41:35.914 27160-27160/com.binox.myapplication I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@f589d3d time:5478528

2 个答案:

答案 0 :(得分:1)

您尚未获得互联网许可,因此首先您必须获得互联网许可。

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

答案 1 :(得分:0)

对您的清单文件给予适当的许可。您的应用需要互联网和位置许可。 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>