我有我的主要活動試圖從導入的包調用方法。我已經發布了下面的電話和方法。我得到的錯誤信息是,該行setContentView導致空指針異常
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)
嘗試使用((活動)上下文).setContentView(twitterSite); –
感謝你排序這個問題。如果你把它作爲答案,我會接受它。你能不能請解釋一下爲什麼這個工作。 – Somk