1

I have build 2 projects my main one and a twitter posting one. I have imported the twitter one into the main and am now trying to hook it up. Below is the import code. The problem is I know I am missing something possibly to do with the context? I have just summarised what is contained in my main project below so you understand better (Hopefully).

import com.tweet.t.TweetToTwitterActivity;

            TweetToTwitterActivity twitter = new TweetToTwitterActivity();
            twitter.buttonLogin(v);

Here is the TweetToTwitterActivity

public class TweetToTwitterActivity extends Activity {

    private SharedPreferences mPrefs;
    /** Twitter4j object */
    private Twitter mTwitter;
    private RequestToken mReqToken;

    public void buttonLogin(View v) {
        mPrefs = getSharedPreferences("twitterPrefs", MODE_PRIVATE);
        // Load the twitter4j helper
        mTwitter = new TwitterFactory().getInstance();

        // Tell twitter4j that we want to use it with our app
        mTwitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
        if (mPrefs.contains(PREF_ACCESS_TOKEN)) {
            Log.i(TAG, "Repeat User");
            loginAuthorisedUser();
        } else {
            Log.i(TAG, "New User");
            loginNewUser();
        }
    }

The Logcat produces this error

09-23 11:27:40.034: ERROR/AndroidRuntime(229): java.lang.NullPointerException
09-23 11:27:40.034: ERROR/AndroidRuntime(229):     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
09-23 11:27:40.034: ERROR/AndroidRuntime(229):     at com.tweet.t.TweetToTwitterActivity.buttonLogin(TweetToTwitterActivity.java:74)

and point to this line.

    mPrefs = getSharedPreferences("twitterPrefs", MODE_PRIVATE);
Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
Somk
  • 11,869
  • 32
  • 97
  • 143
  • where is your onCreate() method.. post full code of this acrtivity so that we get some idea.. – ngesh Sep 23 '11 at 12:12

3 Answers3

3

getSharedPreferences() is a method of the Context class. You are just creating an instance of TweetToTwitterActivity which extends Activity but this activity is not presently started. One way to solve this will be to pass a Context object also to buttonLogin:

Change public void buttonLogin(View v)

to public void buttonLogin(View v, Context context)

and mPrefs = getSharedPreferences("twitterPrefs", MODE_PRIVATE);

to mPrefs = context.getSharedPreferences("twitterPrefs", MODE_PRIVATE);

and twitter.buttonLogin(v);

to twitter.buttonLogin(v, this);

derekerdmann
  • 17,696
  • 11
  • 76
  • 110
Hemant Sharma
  • 249
  • 1
  • 2
2

Hemant is right.

mPrefs = getSharedPreferences("twitterPrefs", MODE_PRIVATE);

for doing this you need TweetToTwitterActivity Activity and not just an instance. So an Activity never gets created when you use new keyword. only OS can create an Activity for ya. so

 twitter.buttonLogin(v, YourCurrentActvity.this);

and

public void buttonLogin(View v, Context context) {
    mPrefs = context.getSharedPreferences("twitterPrefs", MODE_PRIVATE);
    // Load the twitter4j helper
    mTwitter = new TwitterFactory().getInstance();

    // Tell twitter4j that we want to use it with our app
    mTwitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
    if (mPrefs.contains(PREF_ACCESS_TOKEN)) {
        Log.i(TAG, "Repeat User");
        loginAuthorisedUser();
    } else {
        Log.i(TAG, "New User");
        loginNewUser();
    }
}
ngesh
  • 13,398
  • 4
  • 44
  • 60
0

try to pass like this twitter.buttonLogin(v,Activityname.this)

then in called function change like this

public void buttonLogin(View v,Context this)

{ this.getSharedPreferences("twitterPrefs", MODE_PRIVATE);

}

kehnar
  • 1,387
  • 2
  • 12
  • 25