setContentView导致空指针异常

时间:2011-09-26 14:07:03

标签: java android

我的主要活动是尝试从导入的包中调用方法。我已经发布了下面的电话和方法。我得到的错误信息是

的setContentView(twitterSite);

我得到一个零点异常!

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

方法

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

堆栈跟踪

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)

2 个答案:

答案 0 :(得分:7)

setContentViewActivity类的方法。因此,如果您的函数不在活动类中,则必须提及它必须设置内容的活动。

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

这就是你必须将上下文强制转换为Activity的原因。

答案 1 :(得分:0)

onCreate()方法中的第一行必须

super.onCreate(savedInstanceState);