开发者

setContentView causes Null Pointer Exception

I have my main activity trying to call a method from an imported package. I have posted the call and method below. The error message I am getting is that the line

setContentView(twitterSite);

I am getting a null point exception!

            TweetToTwitterActivity twitter = new TweetToTwitterActivity();
            twitter.loginNewUser(v, context开发者_StackOverflow社区);

Method

public void loginNewUser(View v, Context context) {
    mPrefs = context.getSharedPreferences("twitterPrefs", MODE_PRIVATE);
    Log.i(TAG, "Got Preferences");

    // Load the twitter4j helper
    mTwitter = new TwitterFactory().getInstance();
    Log.i(TAG, "Got Twitter4j");

    // Tell twitter4j that we want to use it with our app
    mTwitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);

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

Stacktrace

09-26 08:21:12.123: ERROR/AndroidRuntime(171): Uncaught handler: thread main exiting due to uncaught exception
09-26 08:21:12.312: ERROR/AndroidRuntime(171): java.lang.NullPointerException
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.app.Activity.setContentView(Activity.java:1631)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at com.blundell.tut.ttt.TweetToTwitterActivity.loginNewUser(TweetToTwitterActivity.java:184)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at com.reason.max.Reason$3.onClick(Reason.java:239)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.view.View.performClick(View.java:2344)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.view.View.onTouchEvent(View.java:4133)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.widget.TextView.onTouchEvent(TextView.java:6510)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.view.View.dispatchTouchEvent(View.java:3672)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.app.Dialog.dispatchTouchEvent(Dialog.java:609)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.os.Looper.loop(Looper.java:123)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at android.app.ActivityThread.main(ActivityThread.java:4203)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at java.lang.reflect.Method.invoke(Method.java:521)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
09-26 08:21:12.312: ERROR/AndroidRuntime(171):     at dalvik.system.NativeStart.main(Native Method)


setContentView is a method for Activity class. so if your function is not in activity class you have to mention the activity for which it has to set content.

((Activity) context).setContentView(twitterSite);

That is the reason why you have to typecast context to Activity.


ur first line in onCreate() method must be

super.onCreate(savedInstanceState);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜