Question

I am following this guild http://developer.android.com/training/location/retrieve-current.html#GetLocation

I am trying to convert the fragmentActivity into a fragment but i am having some problems which seem to be context based.

This is what logcat says when i click getLocation:

05-04 12:19:50.949: E/AndroidRuntime(28565): java.lang.IllegalStateException: Could not find a method getLocation(View) in the activity class com.wordpress.yourhappening.happening.MainActivity for onClick handler on view class android.widget.Button with id 'get_location_button'

This is the getLocation method:

public void getLocation(View v) {

    // If Google Play Services is available
    if (servicesConnected()) {

        // Get the current location
        Location currentLocation = mLocationClient.getLastLocation();

        // Display the current location in the UI
        mLatLng.setText(LocationUtils.getLatLng(getActivity(), currentLocation));
    }
}

What i would like to know is what View is represented in getLocation(View v) and how do i properly point it to my fragment because it seems to be looking for the getlocation button inside my main activity.

Was it helpful?

Solution

I am guessing you have this

public void getLocation(View v) {

in Fragment

You need to have this

public void getLocation(View v) 

in Activity.

public static final int onClick

Added in API level 4 Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).

Or in Fragments onCreateView

Initialize button as below and set click listener and get rid of android:onClick:getLocation

Button button =(Button)rootView.findViewById(R.id.get_location_button);
button.setOnClickListener( new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
     // do something

    }
});

If you need Context use getActivity().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top