2014-07-25 55 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

相关问题