wuzg1020 2016-07-31 02:16 采纳率: 5.6%
浏览 1142

编写一个显示当前经纬度的App,textview里面不显示信息?

想在TextView中显示当前经纬度,运行后是空白的(不管是模拟器上还是手机上)代码如下:
package com.example.locationtest;

import java.util.List;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

private TextView positionTextView;
private LocationManager locationManager;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    positionTextView=(TextView)findViewById(R.id.position_text_view);
    locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    List<String>providerList=locationManager.getProviders(true);
    if(providerList.contains(LocationManager.GPS_PROVIDER)){
        provider=LocationManager.GPS_PROVIDER;
    }else if(providerList.contains(LocationManager.NETWORK_PROVIDER)){
        provider=LocationManager.NETWORK_PROVIDER;
    }else {
        Toast.makeText(this, "NO location provider to use",Toast.LENGTH_SHORT).show();
        return;
    }
    Location location=locationManager.getLastKnownLocation(provider);
    if(location!=null){
        showLocation(location);
    }
    locationManager.requestLocationUpdates(provider, 5000, 10, locationListener);
}



@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if(locationManager!=null){
        locationManager.removeUpdates(locationListener);
    }
}

LocationListener locationListener=new LocationListener() {

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

    }

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

    }

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

    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        showLocation(location);
    }
};

    private void showLocation(Location location){
        String currentPosition="latitude is "+location.getLatitude()+"\n"+"longitude is "+location.getLongitude();
        positionTextView.setText(currentPosition);
    }

@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;
}

}

配置文件:
<?xml version="1.0" encoding="utf-8"?>
package="com.example.locationtest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.locationtest.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>


图片说明

  • 写回答

1条回答 默认 最新

  • devmiao 2016-07-31 02:59
    关注
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP