2015-04-22 142 views
2

我正在將Parse集成到現有的Android應用程序中。這就是說,跟隨他們的文檔,我已成功地創建以下的編譯錯誤:錯誤:(31,39)錯誤:無法找到符號變量上下文

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

創建此錯誤的代碼如下。

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); 
    } 
} 

根據解析的documentation我應該叫ParseFacebookUtils.initialize(背景);在我的onCreate方法中,我做了。但是,即使我已經導入了所有相關的類,編譯器仍無法找到符號引用。

通過StackOverflow和Google搜索我知道這是與編譯器無法找到上下文有關,所以我想知道如果我在錯誤的地方初始化它,或者我需要重寫onCreate函數(我認爲我已經做了)。

在此先感謝您的幫助。

+0

你看着http://stackoverflow.com/a/8086225/603270? – shkschneider

+0

這是我發佈我的最後一個問題。 –

回答

4

如果您想傳遞上下文,請使用this(即Activity實例)。

變化

ParseFacebookUtils.initialize(context); 

ParseFacebookUtils.initialize(this); 
+0

解決了這個問題。爲了將來引用其他人(和我自己),你能解釋爲什麼我需要調用這個特定的實例,而不僅僅是上下文嗎?我假設因爲這是第一個電話? –

+1

'context'是一個標識符。你沒有在你的代碼中聲明任何叫做'context'的標識符,這就是你得到這個錯誤的原因。一個Activity是一個Context,這就是爲什麼'this'可以傳遞給'initialize'方法的原因。 – Eran

相關問題