2012-02-03 27 views
1

我寫了一個程序來顯示我的當前位置,在Android手機地址等。使用的API是Android 2.2。以前它在MTS CDMA手機中運行良好。它有一個Android 2.3的API。它也可以在模擬器中正常工作。現在。我買了索尼愛立信Xperia Minipro,它也有Android 2.3。但該程序將運行在它上面。但編輯文本不會顯示任何內容。我使用編輯文本來顯示值。他們沒有被程序更新。可能是什麼問題?相同的程序在模擬器中正常工作,並顯示位置!請幫忙。我可以粘貼下面Android位置無法輸入編輯文本

public class TravelGuide extends Activity { 
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES=1; 
private static final long MINIMUM_TIME_BETWEEN_UPDATES=1000; 
StringBuilder strReturnedAddress; 
List<Address> addresses; 
protected LocationManager locationManager; 
static String videoId; 
Double lat; 
Double longd; 
Address returnedAddress; 

private final LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     //Double latitude = location.getLatitude(); 
     //Double longitude = location.getLongitude(); 

    } 

    @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 

    } 
    }; 
/** Called when the activity is first created. */ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, 
      MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,new MyLocationListener()); 
    showCurrentLocation(); 
} 
public void showCurrentLocation() 
{ 
    Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    if(location!=null) 
    { 
     TextView myLatitude = (TextView)findViewById(R.id.mylatitude); 
     TextView myLongitude = (TextView)findViewById(R.id.mylongitude); 
     TextView myAddress = (TextView)findViewById(R.id.myaddress); 

     String message=String.format("currentLocation\n Longitude:%1$s \n Latitude:%2$s",location.getLongitude(),location.getLatitude()); 
     Toast.makeText(TravelGuide.this,message,Toast.LENGTH_LONG).show(); 
     myLatitude.setText("Latitude: " + String.valueOf(location.getLatitude())); 
      myLongitude.setText("Longitude: " + String.valueOf(location.getLongitude())); 
      Geocoder geocoder = new Geocoder(this, Locale.ENGLISH); 
      try { 
      List<Address> addresses = geocoder.getFromLocation(Double.valueOf(location.getLatitude()), 
        Double.valueOf(location.getLongitude()), 1); 

      if(addresses != null) { 
      Address returnedAddress = addresses.get(0); 
      StringBuilder strReturnedAddress = new StringBuilder("Address:\n"); 
      for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) { 
      strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); 
      } 
     videoId = (returnedAddress.getAddressLine(1)).toString(); 
     //sendAddress(videoId); 
      myAddress.setText(strReturnedAddress.toString()); 
      } 
      else{ 
      myAddress.setText("No Address returned!"); 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      //test = "kovalam"; 
      myAddress.setText("Canont get Address!"); 


    } 

} 

} 


     public class MyLocationListener implements LocationListener 
     { 
      //private final Context MyLocationListener = null; 

      //double LATITUDE; 
      //double LONGITUDE; 
      TextView myLatitude; 
      TextView myLongitude; 
      TextView myAddress; 
     public void onLocationChanged(Location location) 
     { 
      //Context context = null; 
      myLatitude = (TextView)findViewById(R.id.mylatitude); 
      myLongitude = (TextView)findViewById(R.id.mylongitude); 
      myAddress = (TextView)findViewById(R.id.myaddress); 
      //String message=String.format("new Location\n Longitude:%1$s \n Latitude:%2$s", 
      //location.getLongitude(),location.getLatitude()); 
      //Toast.makeText(TouristGuideActivity.this,message,Toast.LENGTH_LONG).show(); 
      myLatitude.setText("Latitude: " + String.valueOf(location.getLatitude())); 
      myLongitude.setText("Longitude: " + String.valueOf(location.getLongitude())); 
      Geocoder geocoder = new Geocoder(TravelGuide.this, Locale.ENGLISH); 
      try { 
      List<Address> addresses = geocoder.getFromLocation(Double.valueOf(location.getLatitude()), 
        Double.valueOf(location.getLongitude()), 1); 

      if(addresses != null) { 
      Address returnedAddress = addresses.get(0); 
      StringBuilder strReturnedAddress = new StringBuilder("Address:\n"); 
      for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) { 
      strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); 
      } 
     videoId = (returnedAddress.getAddressLine(1)).toString(); 
     //sendAddress(videoId); 
      //myAddress.setText(strReturnedAddress.toString()); 
     myAddress.setText("Hello"); 
      } 
      else{ 
      myAddress.setText("No Address returned!"); 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      myAddress.setText("Canont get Address!"); 

     } 
     } 




     public void onStatusChanged(String s, int i, Bundle b) 
     { 
      //Toast.makeText(TouristGuideActivity.this,"Provider status changed",Toast.LENGTH_LONG).show(); 
     } 
     public void onProviderDisabled(String s) 
     { 
      Toast.makeText(TravelGuide.this,"Provider disabled by the user. GPS turned off", 
        Toast.LENGTH_LONG).show(); 
     } 
     public void onProviderEnabled(String s) 
     { 
      Toast.makeText(TravelGuide.this,"Provider enabled by the user. GPS turned on", 
        Toast.LENGTH_LONG).show(); 

     } 


    } 

代碼}

+0

將你的textview對象定義爲這個類的全局變量,並且只需在位置監聽器中設置值之後在onCreate()中啓動類 – Pratik 2012-02-03 05:39:32

+1

我認爲你正在獲取位置對象null。 – MKJParekh 2012-02-03 06:41:36

回答

0

你得到locaton對象null,嘗試Put日誌其他 condition..and是要獲取最後已知位置的原因這在那個手機不可用一些如何.. 所以如果它的真實..

轉到這個答案here實現該代碼來獲取當前位置...和它會正常工作。據我所知,如果你使用GPS Provider獲取Location,它發現它不那麼準確返回你的位置對象,Network Provider是更好的選擇。

+0

抱歉,延遲。我離開了這裏幾天。是位置對象爲空。讓我試試其他代碼。一旦完成,我會在這裏發帖。謝謝! – 2012-02-10 11:24:16