2010-12-18 40 views
0

即時通訊工作在這個應用程序,使用GPS顯示在谷歌地圖上的位置。 Bu似乎有問題,導致應用程序在開始時崩潰,如果在設置中打開了gps,並且以前從未使用過gps。 除此之外,該應用程序似乎工作,因爲它應該應用程序崩潰,如果GPS從未本使用

任何人都可以看到任何可能的原因?

這裏是我的代碼

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); 
    locationManager.requestLocationUpdates(provider, 2000, 500, this);  
    mostRecentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

    } 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    //AdManager.setTestDevices(new String[] { AdManager.TEST_EMULATOR }); 
    super.onCreate(savedInstanceState); 
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS); 
    setContentView(R.layout.main); 
    //getLocation(); 
    Android_ID = Secure.getString(getContentResolver(), Secure.ANDROID_ID); 

      /* Kan man kickstarta GPS såhär ? */ 
      //mostRecentLocation.getLatitude();     
      getLocation(); 


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


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

    if (Locale.getDefault().getLanguage().equals("sv")){ 
    //webView.loadUrl("file:///android_asset/android.html"); 
     webView.loadUrl("file:///android_asset/findgps_sv.html"); 
    }else{ 
    //webView.loadUrl("http://m.bryggplatsen.se/android/polis/index.php"); 
    //webView.loadUrl("file:///android_asset/android_en.html"); 
     webView.loadUrl("file:///android_asset/findgps_en.html"); 
    } 



} 
    /** Sets up the interface for getting access to Latitude and Longitude data from device 
    **/ 




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

    public void sharethisapp(){ 
    startActivity(Intent.createChooser(sharespotIntent(), "Share this warning")); 
    } 
    } 


@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("Close menu"); 
    item = menu.add("Shut down"); 
    item.setIcon(R.drawable.exit); 

    item = menu.add("Share"); 
    item.setIcon(R.drawable.m_share); 


    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    if (item.getTitle() == "Shut down") { 
     System.exit(0); 
     finish(); 
    } 

    if (item.getTitle() == "Share") { 
     if (Locale.getDefault().getLanguage().equals("sv")){ 
     startActivity(Intent.createChooser(shareIntent(), "Dela denna app")); 
     }else{ 
     startActivity(Intent.createChooser(shareIntent(), "Share this app")); 
     } 
    } 
    return true; 
} 

private Intent shareIntent() { 
     Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); 
     shareIntent.setType("text/plain");  
    if (Locale.getDefault().getLanguage().equals("sv")){ 
     shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Testa denna android app..."); 
     return shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://www.bryggplatsen.se/android/polisapp/index.php"); 
    }else{ 
     shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Try this cool android app..."); 
     return shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://www.bryggplatsen.se/android/polisapp/index_en.php");   
    } 

} 


private Intent sharespotIntent() { 
    Intent sharespotIntent = new Intent(android.content.Intent.ACTION_SEND); 
    sharespotIntent.setType("text/plain");  
if (Locale.getDefault().getLanguage().equals("sv")){ 
    sharespotIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, " la just upp en ny varning i appen Polisradar"); 
    return sharespotIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://www.bryggplatsen.se/android/polisapp/warn.php?lat=" + mostRecentLocation.getLatitude() + "&lng=" + mostRecentLocation.getLongitude() + ""); 
}else{ 
    sharespotIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, " just added a new warning in the app Policeradar"); 
    return sharespotIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://www.bryggplatsen.se/android/polisapp/warn_en.php?lat=" + mostRecentLocation.getLatitude() + "&lng=" + mostRecentLocation.getLongitude() + "");  
    } 
} 

}

+0

這可能有助於發佈logcat的。 – iarwain01 2010-12-18 11:32:53

+0

你說得對...病態更新logcat – Jerry 2010-12-18 11:49:36

+0

這裏是logcat,過濾警告和錯誤http://www.bryggplatsen.se/android/polisapp/logcat_eclipse.txt – Jerry 2010-12-18 11:54:23

回答

0

沒有符合您Criteria沒有位置提供商,所以你LocationManager使用位置提供者是null。您需要在代碼中處理這種情況(例如,提示用戶啓用GPS)。

+0

hmm。好,非常感謝,很多谷歌一些,如果我可以找出如何實現, – Jerry 2010-12-18 14:31:38

+0

再次感謝,我GOOGLE了一些,發現這有幫助http://android-er.blogspot.com/2010/10/check-and-prompt-user -to-使-gps.html – Jerry 2010-12-18 15:41:43

0

設置

try { 
       locationManager.requestLocationUpdates(locationManager.getBestProvider(yourcriteria, true), 100, 1, gpsListener); 

      } catch (Exception e) { 
       // TODO: handle exception 
       alertMessage("le recepteur est peut être descativé l'application \n ne peut pas calculer les points GPS \n vous devez les saisir "); 
     } 
相關問題