2011-09-26 103 views
4

我有我的主要活動試圖從導入的包調用方法。我已經發布了下面的電話和方法。我得到的錯誤信息是,該行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) 
+0

嘗試使用((活動)上下文).setContentView(twitterSite); –

+0

感謝你排序這個問題。如果你把它作爲答案,我會接受它。你能不能請解釋一下爲什麼這個工作。 – Somk

回答

7

setContentViewActivity類的方法。所以如果你的功能不在activity類中,你必須提及它必須爲其設置內容的活動。

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

這就是爲什麼你必須對活動進行類型轉換的原因。

+0

這對我的問題來說非常完美,我看過的很多答案都是正確的!謝謝!我遇到了一個問題,那就是我需要在應用程序的開始時選擇一個皮膚,一旦它保存到首選項中,我需要它立即設置皮膚。這是圍繞一個片段持有者的佈局,這就調用了我在MyActivity中創建的方法來設置ContentContent並有效地刷新應用佈局 –

0

onCreate()方法烏爾第一行必須是

super.onCreate(savedInstanceState);