如果我没有使用eclipse DDMS快速发送一些gps,它会在模拟2.1中崩溃。
如果我改变一些代码而不是要求gps位置,它不会崩溃......
我搜索谷歌很多它似乎错误是“getLastKnownLocatio”有时会重新启动null并使其崩溃,但我找不到适合我的代码的解决方法....我不是最好的程序员在世界上:))
请帮帮我......
这是我的java:
public class WebPageLoader extends Activity implements LocationListener{
public static String Android_ID = null;
final Activity activity = this;
private Location mostRecentLocation;
private void getLocation() {
LocationManager locationManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria,true);
//In order to make sure the device is getting the location, request updates.
//locationManager.requestLocationUpdates(provider, 10, 1, this);
//mostRecentLocation = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 1000, 500, this);
mostRecentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
//getLocation();
Android_ID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
/** Allows JavaScript calls to access application resources **/
webView.addJavascriptInterface(new JavaScriptInterface(), "android16");
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Letar poliskontroller");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
getLocation();
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://m.domain.se/android/file.php");
}
/** Sets up the interface for getting access to Latitude and Longitude data from device
**/
private class JavaScriptInterface {
public double getLatitude(){
return mostRecentLocation.getLatitude();
}
public double getLongitude(){
return mostRecentLocation.getLongitude();
}
public String getAndroid_ID(){
return Android_ID;
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
getLocation();
//android16();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem item = menu.add("Meny");
item = menu.add("Stäng app");
item.setIcon(R.drawable.exit);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getTitle() == "Stäng app") {
finish();
}
return true;
}
}
更新
随着你对javacall的消化阻止了崩溃,我也在回收谷歌地图的java中添加了这个
latitude = window.android16.getLatitude();
longitude = window.android16.getLongitude();
if ( isNaN(latitude) ) {
latitude = 61.72680992165949;
} else {
latitude = latitude;
}
if ( isNaN(longitude) ) {
longitude = 17.10124969482422;
} else {
longitude = longitude;
}
现在它似乎工作了......干杯和感谢很多,生病尝试这一点并回到报告,当我把它弄错了一些。
答案 0 :(得分:1)
LocationManager.getLastKnownLocation()返回提供程序的最后已知位置,或 null 。所以在你的JavaScriptInterface中你也应该检查一个null,以免得到NullPointerException。
<强>更新强>:
private class JavaScriptInterface {
public double getLatitude(){
return mostRecentLocation != null ? mostRecentLocation.getLatitude() : Double.NaN;
}
public double getLongitude(){
return mostRecentLocation != null ? mostRecentLocation.getLongitude() : Double.NaN;
}
public String getAndroid_ID(){
return Android_ID;
}
}
此外,您的javascript应该被修复,以便能够处理Double.NaN作为位置不可用的指示。您也可以使用其他常量而不是Double.NaN来表示该状态,它应该超出有效的lon / lat值范围。
答案 1 :(得分:0)
尝试这样的事情,虽然默认情况下返回0(如果mostRecentLocation为null)可能不是你想要的 - 不知道还有什么要做,因为你必须返回一些东西。
private class JavaScriptInterface {
public double getLatitude(){
if (mostRecentLocation != null) // Check for null
return mostRecentLocation.getLatitude();
else
return (Double) 0;
}
public double getLongitude(){
if (mostRecentLocation != null) //Check for null
return mostRecentLocation.getLongitude();
else
return (Double) 0;
}
public String getAndroid_ID(){
return Android_ID;
}
}