将Toast.makeText转换为textView setText

时间:2018-05-26 21:15:01

标签: android google-maps location settext android-fusedlocation

我创建应用程序以获取坐标(如纬度和经度)的用户位置,因此我使用play-service-location,我的程序代码如下:

import android.location.Location; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.location.LocationServices; public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { private Location mLastLocation; private Button btLocation; private GoogleApiClient mGoogleApiClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setupGoogleAPI(); // initialize button btLocation = (Button) findViewById(R.id.bt_getLocation); btLocation.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mLastLocation != null) { Toast.makeText(MainActivity.this," Get Location \n " + "Latitude : "+ mLastLocation.getLatitude()+ "\nLongitude : "+mLastLocation.getLongitude(), Toast.LENGTH_LONG).show(); } } }); } private void setupGoogleAPI(){ // initialize Google API Client mGoogleApiClient = new GoogleApiClient .Builder(this) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); } @Override protected void onStart() { super.onStart(); // connect ke Google API Client ketika start mGoogleApiClient.connect(); } @Override protected void onStop() { super.onStop(); // disconnect ke Google API Client ketika activity stopped mGoogleApiClient.disconnect(); } @Override public void onConnected(Bundle bundle) { // get last location ketika berhasil connect mLastLocation = LocationServices.FusedLocationApi.getLastLocation( mGoogleApiClient); if (mLastLocation != null) { Toast.makeText(this," Connected to Google Location API", Toast.LENGTH_LONG).show(); } } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(ConnectionResult connectionResult) { } }

如上所示,我用toast来显示坐标,问题是当我试验添加setText来显示坐标时,出现了错误

  

错误:没有为setText(double)方法找到合适的方法   TextView.setText(CharSequence)不适用(参数不匹配;   double不能转换为CharSequence)方法   TextView.setText(int)不适用(参数不匹配;可能   从double到int的有损转换

我已经尝试了几天,结果总是出错,我如何在setText中显示坐标,所以在textView中可以看到坐标?

2 个答案:

答案 0 :(得分:0)

Toast的问题在于它使用双打而不是字符串,因此无法设置

  

"获取位置\ n" +                               "纬度:" + mLastLocation.getLatitude()+                               " \ n长度:" + mLastLocation.getLongitude()

作为您的textview。尝试将mLastLocation.getLatitude()mLastLocation.getLongitude()转换为字符串,然后再将其设置为textView。检查此答案(here

答案 1 :(得分:0)

这个问题只是将double值转换为预期类型的​​问题 - String可以很好地完成。 Java为此提供了几种方法:

double lat = mLastLocation.getLatitude();

String latString1 = String.valueOf(lat);
String latString2 = Double.toString(lat);

甚至只是简单快速和肮脏(经常皱眉!):

String latString3 = "" + lat;

现在只需使用setText(),就像这样:

myTextView.setText(latStringX);

“X”,您选择的变体。

转化的一般评论:
如果您要转换的值是(出于某种原因)null,那么 String.valueOf()会返回null,但Double.toString()会抛出NullPointerException