2

I am integrating Parse into an existing Android application. That said, following their documentation, I have managed to create a compile error of the following:

Error:(31, 39) error: cannot find symbol variable context

The code that creates this error is below.

import android.content.Intent;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.parse.Parse;
import com.parse.ParseFacebookUtils;

public class MainActivity extends ActionBarActivity {
    // Create, automatically open (if applicable), save, and restore the
    // Active Session in a way that is similar to Android UI lifecycles.
    private UiLifecycleHelper uiHelper;
    private View otherView;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Parse.initialize(this, "PARSE APPLICATION ID REMOVED FOR SECURITY REASONS", "PARSE CLIENT ID REMOVED FOR SECURITY REASONS");
        ParseFacebookUtils.initialize(context);
        setContentView(R.layout.activity_main);
        // Set View that should be visible after log-in invisible initially
        otherView = (View) findViewById(R.id.other_views);
        otherView.setVisibility(View.GONE);
        // To maintain FB Login session
        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);
    }

    // Called when session changes
    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state,
                         Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

    // When session is changed, this method is called from callback method
    private void onSessionStateChange(Session session, SessionState state,
                                      Exception exception) {
        final TextView name = (TextView) findViewById(R.id.name);
        final TextView gender = (TextView) findViewById(R.id.gender);
        final TextView location = (TextView) findViewById(R.id.location);
        // When Session is successfully opened (User logged-in)
        if (state.isOpened()) {
            Log.i(TAG, "Logged in...");

            //Opens new activity view so user can edit profile.
            startActivity(new Intent(MainActivity.this, SetupProfileActivity.class));

            /*// make request to the /me API to get Graph user
            Request.newMeRequest(session, new Request.GraphUserCallback() {

                // callback after Graph API response with user
                // object
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if (user != null) {
                        // Set view visibility to true
                        otherView.setVisibility(View.VISIBLE);
                        // Set User name
                        name.setText("Hello " + user.getName());
                        // Set Gender
                        gender.setText("Your Gender: "
                                + user.getProperty("gender").toString());
                        location.setText("Your Current Location: "
                                + user.getLocation().getProperty("name")
                                .toString());
                    }
                }
            }).executeAsync();*/

        } else if (state.isClosed()) {
            Log.i(TAG, "Logged out...");
            otherView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ParseFacebookUtils.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
        Log.i(TAG, "OnActivityResult...");
    }

    @Override
    public void onResume() {
        super.onResume();
        uiHelper.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }
}

According to Parse's documentation I am supposed to call ParseFacebookUtils.initialize(context); in my onCreate method, which I did. However the compiler cannot find the symbol reference even though I have imported all relevant classes.

Searching through StackOverflow and Google I know this is related to the compiler not being able to find the context so I am wondering if I am initializing it in the wrong place or do I need to override the onCreate function (which I think I already did).

Thanks in advance for your help.

cp-stack
  • 785
  • 3
  • 17
  • 40

1 Answers1

4

If you want to pass a Context, use this (i.e. the Activity instance).

change

ParseFacebookUtils.initialize(context);

to

ParseFacebookUtils.initialize(this);
Eran
  • 387,369
  • 54
  • 702
  • 768
  • That fixed the issue. For future reference to others (and myself), could you explain why I needed to call this specific instance instead of just the context? I am assuming because this was the first call? – cp-stack Apr 22 '15 at 15:14
  • 1
    `context` is an identifier. You didn't declare any identifier called `context` in your code, which is why you got this error. An Activity is a Context, which is why `this` can be passed to that `initialize` method. – Eran Apr 22 '15 at 15:15