Java gps在模拟器中工作,但在移动设备中不工作
您好,使用简单的android应用程序显示用户地址,但该应用程序在emulator中运行良好,但当我将其安装在移动设备中时,其显示力关闭。请帮助我,提前感谢Java gps在模拟器中工作,但在移动设备中不工作,java,android,Java,Android,您好,使用简单的android应用程序显示用户地址,但该应用程序在emulator中运行良好,但当我将其安装在移动设备中时,其显示力关闭。请帮助我,提前感谢 package com.gps.com; 导入java.io.IOException; 导入java.util.List; 导入java.util.Locale; 导入android.app.Activity; 导入android.content.Context; 导入android.location.Address; 导入android.
package com.gps.com;
导入java.io.IOException;
导入java.util.List;
导入java.util.Locale;
导入android.app.Activity;
导入android.content.Context;
导入android.location.Address;
导入android.location.Geocoder;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
公共类主要扩展活动{
私有静态最终最短长\u距离\u更改\u用于\u更新=1;//以米为单位
私有静态最终最长最短\u更新间隔时间\u=1000;//以毫秒为单位
受保护的LocationManager LocationManager;
受保护的按钮检索速度按钮;
公共场所;
公共图文电视;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
retrieveLocationButton=(按钮)findViewById(R.id.retrieve\u location\u按钮);
tv=(TextView)findviewbyd(R.id.tv);
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
locationManager.RequestLocationUpdate(
LocationManager.GPS\u提供程序,
两次更新之间的最短时间,
用于更新的最小距离更改,
新建MyLocationListener()
);
retrieveLocationButton.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
showCurrentLocation();
}
});
}
public void showCurrentLocation(){
Location Location=locationManager.getLastKnownLocation(locationManager.GPS\U提供程序);
如果(位置!=null){
字符串消息=String.format(
“当前位置\n经度:%1$s\n纬度:%2$s”,
location.getLength(),location.getLatitude()
);
Toast.makeText(main.this,message,
Toast.LENGTH_LONG).show();
}
试一试{
列出地址;
Geocoder gc=新的地理编码器(this,Locale.ENGLISH);
addresses=gc.getFromLocation(location.getLatitude(),location.getLatitude(),1);
如果(地址!=null){
地址currentAddr=地址。获取(0);
StringBuilder sb=新的StringBuilder(“地址:\n”);
对于(inti=0;i这条线
addresses = gc.getFromLocation(location.getLatitude(),location.getLongitude(), 1);
可能会给您一个Nullpointer异常,因为它在if块之外,未处理,因为您只在该特定的try块中捕获IOException。
这可能不是您遇到的问题的解决方案,但我想我还是要指出这一点。这也是一个明显的建议,但您不能以编程方式打开GPS,您必须手动打开。您是否已验证手机中的GPS已打开???我也有相同的问题。请在manifest.xml中尝试此代码。它可以正常工作对我来说
主要活动:
public class WhereIActivity extends MapActivity {
// MapController mc;
MapView myMapView;
MapController mapController;
GeoPoint point;
MyPositionOverlay positionOverlay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_where_i);
MapView myMapView=(MapView)findViewById(R.id.myMapView);
mapController=myMapView.getController();
myMapView.displayZoomControls(false);
mapController.setZoom(17);
positionOverlay=new MyPositionOverlay();
List<Overlay> overlays=myMapView.getOverlays();
overlays.add(positionOverlay);
// myMapView.setSatellite(true);
myMapView.setStreetView(true);
myMapView.setTraffic(true);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria crta = new Criteria();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(false);
crta.setBearingRequired(false);
crta.setCostAllowed(true);
crta.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(crta, true);
// String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
}
private final LocationListener locationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
private void updateWithNewLocation(Location location) {
String latLong;
TextView myLocation;
myLocation = (TextView) findViewById(R.id.myLocation);
String addressString = "no address found";
if(location!=null) {
positionOverlay.setLocation(location);
Double geoLat=location.getLatitude()*1E6;
Double geoLng=location.getLongitude()*1E6;
GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue());
mapController.animateTo(point);
double lat = location.getLatitude();
double lon = location.getLongitude();
latLong = "Lat:" + lat + "\nLong:" + lon;
double lattitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this,Locale.getDefault());
try {
List<Address> addresses= gc.getFromLocation(lattitude, longitude, 1);
StringBuilder sb = new StringBuilder();
if(addresses.size()>0) {
Address address=addresses.get(0);
for(int i=0;i<address.getMaxAddressLineIndex();i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
}
catch (Exception e) {
}
} else {
latLong = " NO Location Found ";
}
myLocation.setText("Current Position is :\n"+ latLong + "\n"+ addressString );
}
Main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".WhereIActivity" >
<com.google.android.maps.MapView
android:id="@+id/myMapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0_MW1zfGUXAZ2cAEBaB62GeWv0FoZODowBf1WYg"
/>
<TextView
android:id="@+id/myLocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
tools:context=".WhereIActivity"
android:gravity="center_horizontal"
android:textStyle="bold"
android:textSize="14sp"
android:typeface="sans"
android:textColor="#0099CC"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.wherei"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps"></uses-library>
<activity
android:name=".WhereIActivity"
android:label="@string/title_activity_where_i" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
</manifest>
共享强制关闭错误登录我在emulator中没有收到任何错误,它在emulator中工作正常,但当我在mobile中运行它时,它正在强制关闭。是的,我打开了GPS,但无法获取地址。请帮助我我们这里我需要修改代码。请发送代码我的邮件id:chaitanyasai508@gmail.com谢谢你,我正在努力但是它的表演需要15个名声。请帮助我,卡蒂。plz@user1515332:查看我编辑的答案。这将对你有帮助。我只使用了此代码。它工作正常。谢谢你,karthi我收到了。非常感谢,我接受你的答案
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".WhereIActivity" >
<com.google.android.maps.MapView
android:id="@+id/myMapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0_MW1zfGUXAZ2cAEBaB62GeWv0FoZODowBf1WYg"
/>
<TextView
android:id="@+id/myLocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
tools:context=".WhereIActivity"
android:gravity="center_horizontal"
android:textStyle="bold"
android:textSize="14sp"
android:typeface="sans"
android:textColor="#0099CC"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.wherei"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps"></uses-library>
<activity
android:name=".WhereIActivity"
android:label="@string/title_activity_where_i" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
</manifest>