这是源代码
package walk.a;import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.os.Bundle;//package org.crazyit.gps;import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.EditText;
public class WalkActivity extends Activity
{
// 定义LocationManager对象
LocationManager locManager;
// 定义程序界面中的EditText组件
EditText show;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取程序界面上的EditText组件
show = (EditText) findViewById(R.id.show);
// 创建LocationManager对象
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 从GPS获取最近的最近的定位信息
Location location = locManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER);
// 使用location根据EditText的显示
updateView(location);
// 设置每3秒获取一次GPS的定位信息
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER
, 3000, 8, new LocationListener()
{
@Override
public void onLocationChanged(Location location)
{
// 当GPS定位信息发生改变时,更新位置
updateView(location);
} @Override
public void onProviderDisabled(String provider)
{
updateView(null);
} @Override
public void onProviderEnabled(String provider)
{
// 当GPS LocationProvider可用时,更新位置
updateView(locManager
.getLastKnownLocation(provider));
} @Override
public void onStatusChanged(String provider, int status,
Bundle extras)
{
}
});
} // 更新EditText中显示的内容
public void updateView(Location newLocation)
{
if (newLocation != null)
{
StringBuilder sb = new StringBuilder();
sb.append("实时的位置信息:\n");
sb.append("经度:");
sb.append(newLocation.getLongitude());
sb.append("\n纬度:");
sb.append(newLocation.getLatitude());
sb.append("\n高度:");
sb.append(newLocation.getAltitude());
sb.append("\n速度:");
sb.append(newLocation.getSpeed());
sb.append("\n方向:");
sb.append(newLocation.getBearing());
show.setText(sb.toString());
}
else
{
// 如果传入的Location对象为空则清空EditText
show.setText("");
}
}
}
一点都没有改动,只是改了改类的名字,但是运行的时候,就是会报错,模拟器说意外停止,请重试。真心不知道到底哪里有错误……求大神……
package walk.a;import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.os.Bundle;//package org.crazyit.gps;import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.EditText;
public class WalkActivity extends Activity
{
// 定义LocationManager对象
LocationManager locManager;
// 定义程序界面中的EditText组件
EditText show;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取程序界面上的EditText组件
show = (EditText) findViewById(R.id.show);
// 创建LocationManager对象
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 从GPS获取最近的最近的定位信息
Location location = locManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER);
// 使用location根据EditText的显示
updateView(location);
// 设置每3秒获取一次GPS的定位信息
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER
, 3000, 8, new LocationListener()
{
@Override
public void onLocationChanged(Location location)
{
// 当GPS定位信息发生改变时,更新位置
updateView(location);
} @Override
public void onProviderDisabled(String provider)
{
updateView(null);
} @Override
public void onProviderEnabled(String provider)
{
// 当GPS LocationProvider可用时,更新位置
updateView(locManager
.getLastKnownLocation(provider));
} @Override
public void onStatusChanged(String provider, int status,
Bundle extras)
{
}
});
} // 更新EditText中显示的内容
public void updateView(Location newLocation)
{
if (newLocation != null)
{
StringBuilder sb = new StringBuilder();
sb.append("实时的位置信息:\n");
sb.append("经度:");
sb.append(newLocation.getLongitude());
sb.append("\n纬度:");
sb.append(newLocation.getLatitude());
sb.append("\n高度:");
sb.append(newLocation.getAltitude());
sb.append("\n速度:");
sb.append(newLocation.getSpeed());
sb.append("\n方向:");
sb.append(newLocation.getBearing());
show.setText(sb.toString());
}
else
{
// 如果传入的Location对象为空则清空EditText
show.setText("");
}
}
}
一点都没有改动,只是改了改类的名字,但是运行的时候,就是会报错,模拟器说意外停止,请重试。真心不知道到底哪里有错误……求大神……
别的没加,不知道是不是这个的原因
2.查看一下你的gps provider在setting中有没有enable.
再加上 internet权限和 access coarse location权限。
如果还是不行,那一定是程序的问题。贴log。
07-09 02:16:30.960: E/AndroidRuntime(817): Uncaught handler: thread main exiting due to uncaught exception
07-09 02:16:30.981: E/AndroidRuntime(817): java.lang.RuntimeException: Unable to start activity ComponentInfo{walk.a/walk.a.WalkActivity}: java.lang.SecurityException: Requires ACCESS_FINE_LOCATION permission
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" > </uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
都放进去试下
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />要写正确另外,模拟器没有定位功能
报这样的错:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hr.gpsandroid/com.hr.gpsandroid.GpsActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class uses-permissionCaused by: java.lang.ClassNotFoundException: android.view.uses-permission in loader dalvik.system.PathClassLoader[/data/app/com.hr.gpsandroid-1.apk]