2014-07-25 56 views
0

我想通過點擊一個按鈕來顯示tvLatitudetvLongitude(它們都是TextView's)的經度和緯度。它不適用於我目前擁有的代碼,你可以看到下面的代碼。問題是,當控制到達字符串latitude = Double.toString(loc.getLatitude());時,程序不幸地停止。 誰能告訴我爲什麼loc.getLatitudeloc.getLongitude沒有響應?onLocationChanged()從未在Android中調用過

package mk.bukhari.loctrack; 

import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.Fragment; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.os.Build; 

public class MainActivity extends ActionBarActivity implements LocationListener { 

    TextView tvLat, tvLon; 
    Button btnLoc; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.container, new PlaceholderFragment()).commit(); 
      tvLat = (TextView) findViewById(R.id.tvLatitude); 
      tvLon = (TextView) findViewById(R.id.tvLongitude); 
      btnLoc = (Button) findViewById(R.id.btnLocation); 

      final LocationManager locman = (LocationManager) 
      getSystemService(LOCATION_SERVICE); 
      locman.requestLocationUpdates("gps", 0, 0, this); 

      btnLoc.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        Log.d("LocTrack", "Button Clicked"); 
        Location loc = locman.getLastKnownLocation("gps"); 
        onLocationChanged(loc); 
       } 
      });   
     } 
    } 
} 

    @Override 
    public void onLocationChanged(Location loc) { 
     Log.d("LocTrack", "Entering onLacationChanged"); 
     String latitude = Double.toString(loc.getLatitude()); 
     String longitude = Double.toString(loc.getLongitude()); 
     Log.d("LocTrack", "Setting txt in tv"); 
     tvLat.setText(latitude); 
     tvLon.setText(longitude); 
    } 

    @Override 
    public void onProviderDisabled(String arg0) {} 

    @Override 
    public void onProviderEnabled(String arg0) {} 

    @Override 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {} 
} 
+0

直到Log.d( 「LocTrack」, 「進入onLocationChanged」);此行程序按預期執行,輸入onLocationChanged在logcat中顯示,但應用程序停在它旁邊,並顯示一個對話框,指出「不幸LocTrak已停止」 –

+0

您可以發佈您的logcat嗎?所以我們可以知道有什麼問題 –

+0

對不起兄弟圖片不上傳指導我如何提供此圖片 –

回答

0

好吧,看來你的位置返回null,嘗試改變你的代碼是這樣的:

@Override 
public void onLocationChanged(Location loc) { 
    Log.d("LocTrack", "Entering onLacationChanged"); 

    if(loc != null) { //Add this validation 
     String latitude = Double.toString(loc.getLatitude()); 
     String longitude = Double.toString(loc.getLongitude()); 
     Log.d("LocTrack", "Setting txt in tv"); 
     tvLat.setText(latitude); 
     tvLon.setText(longitude); 
    } else //If loc is null then inform the user 
     Toast.makeText(this, "Unable to find your location, try again", Toast.LENGTH_SHORT).show(); 
} 
+0

是的兄弟你是對的。其他部分正在執行。我現在該怎麼辦 –

+0

CarlosJimenez loc正在返回null。請指導我現在應該做什麼。爲什麼loc是空的 –

+0

檢查[this](http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/)教程。我確信它會幫助你 –

0

好像你使用了錯誤的供應商。

嘗試調用

locman.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0 , 0, this); 

,然後在onClick(View v)

Location loc = locman.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
+0

好吧兄弟我在更改後立即承認您 –

+0

我替換了代碼,但locman.requestLocationUpdates(LocationManager。GPS_PROVIDER);給出錯誤並建議爲其添加參數 –

+0

我已編輯答案中的代碼。請再試一次。 – shyam

相關問題