Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid context issues from imported package
    text
    copied!<p>I have a twitter package that runs fine on its own. Now when I import it and try to use it in my main app I hit upon issues of nullpointerexceptions. The first I solved, but immediately ran into another. I now believe it is because I am handling the context incorrectly.</p> <p>Here is how I have called one method from my included package.</p> <pre><code> TweetToTwitterActivity twitter = new TweetToTwitterActivity(); twitter.buttonLogin(v, context); </code></pre> <p>Now in the TweetToTwitterActivity file I have this. The first method runs fine but I am now getting a null pointer exception from this line. I think I am going about this completely the wrong way. Can anyone help me understand how to run methods properly from an imported class?</p> <blockquote> <p>setContentView(twitterSite);</p> </blockquote> <pre><code>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(context); } } private void loginNewUser(Context context) { try { Log.i(TAG, "Request App Authentication"); mReqToken = mTwitter.getOAuthRequestToken(CALLBACK_URL); Log.i(TAG, "Starting Webview to login to twitter"); WebView twitterSite = new WebView(context); twitterSite.requestFocus(View.FOCUS_DOWN); twitterSite.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_UP: if (!v.hasFocus()) { v.requestFocus(); } break; } return false; } }); twitterSite.loadUrl(mReqToken.getAuthenticationURL()); setContentView(twitterSite); } catch (TwitterException e) { Log.e("HelloWorld", "Error in activity", e); Toast.makeText(this, "Twitter Login error, try again later", Toast.LENGTH_SHORT).show(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload