0

I'm following this example from http://developer.android.com to build a simple app to find the last known location. I get the cannot find symbol variable error for the variables, mLastLocation, mLatitudeText and mLongitudeText in the following code snippet in the activity file:

    @Override
    public void onConnected(Bundle connectionHint) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {
            mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
            mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
        }
    }

The variables aren't defined in the sample too. How can I fix this?

2 Answers 2

1

You need to define the two mentioned TextView widgets in your UI or main_activity.xml file

....
<TextView
    android:text="-"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textview1"
    />

  <TextView
    android:text="-"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textview2"
    />
....

..and then reference them in the code in your MainActivity.java file. For mLastLocation, well it needs to be Location type, since the function getLastLocation returns a Location type.

public class MainActivity extends ActionBarActivity implements
    ConnectionCallbacks, OnConnectionFailedListener {

Location mLastLocation;
TextView mLatitudeText,mLongitudeText;

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

   mLatitudeText=(TextView)findViewById(R.id.textview1);
   mLongitudeText=(TextView)findViewById(R.id.textview2); 
 }


...
@Override
public void onConnected(Bundle connectionHint) {
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {
        mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
        mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

The link to the android developers website isn't pointing to a specific tutorial, so I can speak directly to that one. However, the mLatitudeText and mLongitudeText appear to be simple TextViews. If they aren't created, you can just create them and add them to the activity yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.