2010-11-28 12 views
1

私はかなりこの問題を解決する方法をshureしないアンドロイドで開発することに新しいです。 私は自分のアプリをリリースし、Android 2.1がクラッシュするいくつかのpplからのレポートを取得し始めました。私のHTC Desire 2.2で正常に動作し、シミュレーションで動作するように見える1.6getLastKnownLocationクラッシュアンドロイド2.1

と私はすぐにeclipse DDMSでいくつかのGPSを送信しなければシミュレートされた2.1でクラッシュします。

と私はGPS位置を求めるためにはないいくつかのコードを変更した場合、それはクラッシュしていない...

私はそれにたくさんGoogle検索し、エラーは「getLastKnownLocatioは」時々ヌルretrnsということで、それはそれを作るようですクラッシュが、私はそれは私のコードでフィットするための回避策を見つける....イムない世界で最高のプログラマ傾ける:)

私を助けてください...ここで

は私の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のウィッヒのためのあなたの暗示で

}

UPDATE

は、クラッシュを防ぎ、また、私は今、動作しているようrecivingグーグルマップ

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

のJavaでこれを追加しました。 ..乾杯してくれてありがとう、たくさんの人がこれを試してみて、私がそれをいくつかバグを起こしたときに報告してください。

答えて

1

LocationManager.getLastKnownLocation()は、プロバイダの最後の既知の場所、またはnullを返します。 JavaScriptInterfaceでは、NullPointerExceptionを受け取らないようにnullをチェックする必要があります。

UPDATE

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値の範囲外でなければなりません。

+0

repliさん、ありがとうございました。どうすればいいですか?あなたは私に少しの例を与えることができますか? – Jerry

+1

@ジェリー - UPDATEセクションを参照してください。 –

+0

はい - 私の例よりもエレガントです。私はNaNを忘れていました(私はかなりJavaに新しいです) – Squonk

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; 
     } 
} 
+0

あなたの返事をお寄せいただきありがとうございました。それらは両方とも "NaN"でクラッシュするのを防止しています... 私は、 DDMS – Jerry

+0

私はそれが仕事に持っていると思う...私の更新。 – Jerry