2012-02-27 2 views
1

は私がツイッターからの情報にアクセスしようと、私は、このリンクをたどっ:android.content.Contextとandroid.app.Activityでコンテキストを作成するには?

http://code.google.com/p/sociallib/wiki/SocialLibGuide

は、私は次の2行を理解していないと、それは次の行でエラーが表示されます。

 twitter.requestAuthorization(this); 
     twitter.authorize(this); 

完全なコードが以下に追加されました。

anroid.content.Contextとandroid.app.Activityが必要です。私は本当にそれらを追加する方法を知りません。どんな助けもありがとうございます。

package sociallibjar; 

import android.R; 
import android.content.Context; 
import com.expertiseandroid.lib.sociallib.connectors.SocialNetworkHelper; 
import com.expertiseandroid.lib.sociallib.connectors.TwitterConnector; 
import com.expertiseandroid.lib.sociallib.model.twitter.TwitterUser; 
import org.scribe.oauth.Token; 

public class TwitterApp { 

    String CONS_KEY = ""; 
    String CONS_SEC = ""; 
    String CALLBACK = ""; 

    public void twiter() { 
     try { 
      TwitterConnector twitter = SocialNetworkHelper.createTwitterConnector(CONS_KEY, CONS_SEC, CALLBACK); 
      twitter.requestAuthorization(this); 
      twitter.authorize(this); 
      Token at = twitter.getAccessToken(); 
      String token = at.getToken(); //You can store these two strings 
      String secret = at.getSecret();//in order to build the token back 


      Token myAccessToken = new Token(token, secret); 
      twitter.authentify(myAccessToken); 
      TwitterUser me = twitter.getUser(); //Retrieves the current user 
      String nickname = me.getUsername(); //Some information is available through method calls 
      int nbFollowers = me.nbFollowers; //Some is available through object fields 

      System.out.println("You are logged in as " + nickname + " and you have " + nbFollowers + " followers"); //Now we can print the information we retrieved onscreen 

      twitter.tweet("Tweeting from code"); //A simple tweet 

      twitter.tweet("I have " + twitter.getUser().nbFollowers + " followers!");//A more elaborate tweet 
      //twitter.logout(this); //Providing this code is located in an Activity (or Context) class 


     } catch (Exception e) { 
     } 

    } 


} 
+0

場所の2行が書き込まれているように、もう少しコードを表示してください。 – MByD

答えて

2

このリンクのドキュメントはかなり明確です。ラインについて:

twitter.requestAuthorization(this); //Providing this code is located in an Activity (or Context) class 
twitter.authorize(this); 
this

Context(例えばActivity)を表します。 クラスTwitterAppActivityを拡張していないため、これらのメソッドを提供するにはContextへの参照が必要です。このためにあなたは、たとえば、Contextをとり、あなたのTwitterAppクラスにコンストラクタを追加することができます。

private Context ctx; //-<field in the TwitterApp class 

public TwitterApp (Context ctx) { 
    this.ctx = ctx; 
} 

、その後、あなたはそれらのメソッドのために、そのコンテキストを使用する:あなたはインスタンス化し、あなたの活動に

twitter.requestAuthorization(ctx); 
twitter.authorize(ctx); 

TwitterAppクラスは、ちょうどthisを渡す:

TwitterApp obj = new TwitterApp(this); 
+0

ありがとうslukian、アンドロイドの外でプログラムを実行できますか?なぜなら私はプログラムを実行しようとすると次の例外が発生するからです。スレッド "main"の例外java.lang.RuntimeException:Stub! android.content.Contextの (Context.java:4) (ContextWrapper.java:5) android.view.ContextThemeWrapperにあります。 (ContextThemeWrapper.java:5) android.app.Activity。 (Activity.java:6) (sociallibjar.TwitterApp)。 (TwitterApp.java:19) – SAR

+0

@ブラダン「外のアンドロイド」の意味を正確にはわかりませんが、答えは「いいえ」です。あなたはアンドロイド用のライブラリデザインを使用していますので、アンドロイドシステムへのリファレンスが 'Context'の形で必要です。 – Luksprog

+0

私はそれを今得ました。 outside => android os以外 – SAR