开发者

Android context issues from imported package

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.

Here is how I have called one method from my included package.

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

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?

setContentView(twitterSite);

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 Vie开发者_如何学Gow.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();
    }
}


To display the web view, Insure TweetToTwitterActivity twitter is a started and active activity, otherwise it can not setContentView, as it is not in control. This can be done with something like...

TweetToTwitterActivity twitter = new TweetToTwitterActivity();
Intent intent = new Intent();
intent.setClass(context, TweetToTwitterActivity .class);
startActivity(intent);

Then, it is likely best (possibly required) to call your method form the twitter activity onCreate method, rather then from where the activity is created.

If, as mentioned in the comment, you do not want to actually display the webview, but simply call a web page and analyze the results, do not use a webview, but rather an HttpPost (Or possibly an HttpGet, depending on what you are doing.) Here is an example from an app I have that does a web post, and looks at the results rather then displaying the results....

public static void checkWebSite() {
    HttpClient httpclient = getClient();
    HttpPost httppost = new HttpPost("http://yoursite.com/pathtopage");
    try {
                    //If you need to add name value params you can do that here...
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("name", "value"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        String html = InputStreamToString(response.getEntity().getContent()).toString();

        //You can now analyze the html string for whatever you are looking for

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

}   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜