-2

I create apps to get user locations in coordinate like latitude and longitude, so I use play-service-location, my program code is below:


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) {

    }
}

as seen above, I use toast to display the coordinates, the problem is when I experiment with adding setText to display coordinates, there is an error

error: no suitable method found for setText(double) method TextView.setText(CharSequence) is not applicable (argument mismatch; double cannot be converted to CharSequence) method TextView.setText(int) is not applicable (argument mismatch; possible lossy conversion from double to int

I have tried several days and the result is always error, how I display the coordinates in setText, so the coordinates can be seen in textView?

Hamid Yusifli
  • 9,688
  • 2
  • 24
  • 48
laura
  • 13
  • 3

2 Answers2

0

The problem with your Toast is that it uses doubles instead of strings therefore you cannot set

" Get Location \n " + "Latitude : "+ mLastLocation.getLatitude()+ "\nLongitude : "+mLastLocation.getLongitude()

as your textview. Try converting mLastLocation.getLatitude()and mLastLocation.getLongitude() to strings before setting it as a textView. Check this answer out(here)

Kristofer
  • 809
  • 9
  • 24
0

This issue is just a simply matter of casting your double values to the expected type--String will do nicely. Java provides several methods for this:

double lat = mLastLocation.getLatitude();

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

or even simply quick and dirty (often frown upon!):

String latString3 = "" + lat;

Now simply use setText() like this:

myTextView.setText(latStringX);

"X" for which ever variation you choose.

General Comment on Conversion:
If the value you wish to convert is (for some reason) null, then String.valueOf() will return null, but Double.toString() will throw a NullPointerException.

Barns
  • 4,850
  • 3
  • 17
  • 31
  • remain the same, it gets java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference – laura May 27 '18 at 00:20
  • @laura :: According to your statement your were getting the error `setText(double) method TextView.setText(CharSequence) is not applicable (argument mismatch; double cannot be converted to CharSequence)`, but now you say you get `NullPointerException`. Please read my answer carefully! It states that if you try to convert `null` with `Double.toString()` you get a `NullPointerException`. When you try to convert `null` with `String.valueOf()` you get `null` return. – Barns May 27 '18 at 01:13
  • @laura :: please edit your original post and show exactly how your are using `setText()`, on which object, how and where you declare that object. – Barns May 27 '18 at 01:14