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 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题