2013-05-02 63 views
0

我使用的是從對話框,包含網絡視圖組件Twitter的API的Android Twitter的登錄Looper.prepair

這個錯誤來自logcat的 「不能創建內螺紋的處理程序進行登錄到我的Android應用程序,沒有Looper.prepare()「 這個錯誤的解決方案是什麼? 這是OAuthRequestTokenTask.java類的代碼

import com.noqwerty.m.LoginActivity; 

import oauth.signpost.OAuthConsumer; 
import oauth.signpost.OAuthProvider; 
import android.content.Context; 
import android.os.AsyncTask; 
import android.os.Looper; 
import android.util.Log; 
public class OAuthRequestTokenTask extends AsyncTask<Void, Void, Void> { 
    final String TAG = getClass().getName(); 
    private Context context; 
    private OAuthProvider provider; 
    private OAuthConsumer consumer; 
    public OAuthRequestTokenTask(Context context, OAuthConsumer consumer, 
      OAuthProvider provider) { 
     this.context = context; 
     this.consumer = consumer; 
     this.provider = provider; 
    } 
    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      Log.i(TAG, "Retrieving request token from Google servers"); 
      final String url = provider.retrieveRequestToken(consumer, 
        Constants.OAUTH_CALLBACK_URL); 
      Log.i(TAG, "Popping a browser with the authorize URL : " + url); 
      // Intent intent = new Intent(Intent.ACtuTION_VIEW, 
      // Uri.parse(url)).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | 
      // Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND); 
      // context.startActivity(intent); 
      // Looper.prepare(); 
      LoginActivity.twd.setUpWebView(url); 

     } catch (Exception e) { 
      Log.e(TAG, "Error during OAUth retrieve request token", e); 
     } 
     return null; 
    } 
} 

這TwitterDialog.java類

import oauth.signpost.OAuthConsumer; 
import oauth.signpost.OAuthProvider; 
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; 
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider; 
import oauth.signpost.http.HttpParameters; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Display; 
import android.view.ViewGroup; 
import android.view.Window; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.FrameLayout; 
import android.widget.LinearLayout; 
import android.widget.TextView; 


public class TwitterDialog extends Dialog { 

    static final float[] DIMENSIONS_LANDSCAPE = { 460, 260 }; 
    static final float[] DIMENSIONS_PORTRAIT = { 280, 420 }; 
    static final FrameLayout.LayoutParams FILL = new FrameLayout.LayoutParams(
      ViewGroup.LayoutParams.FILL_PARENT, 
      ViewGroup.LayoutParams.FILL_PARENT); 
    static final int MARGIN = 4; 
    static final int PADDING = 2; 
    private ProgressDialog mSpinner; 
    private WebView mWebView; 
    private LinearLayout mContent; 
    private TextView mTitle; 
    private boolean progressDialogRunning = false; 
    final String TAG = getClass().getName(); 
    private OAuthConsumer consumer; 
    private OAuthProvider provider; 
    HttpParameters params1; 

    public TwitterDialog(Context context) { 
     super(context); 
     try { 
      this.consumer = new CommonsHttpOAuthConsumer(
        Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET); 
      this.provider = new CommonsHttpOAuthProvider(Constants.REQUEST_URL, 
        Constants.ACCESS_URL, Constants.AUTHORIZE_URL); 
     } catch (Exception e) { 
      Log.e(TAG, "Error creating consumer/provider", e); 
     } 

     Log.i(TAG, "Starting task to retrieve request token."); 
     new OAuthRequestTokenTask(this.getContext(), consumer, provider) 
       .execute(); 
    } 

// @Override 
// protected void onCreate(Bundle savedInstanceState) { 
//  super.onCreate(savedInstanceState); 
//  try { 
//   this.consumer = new CommonsHttpOAuthConsumer(
//     Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET); 
//   this.provider = new CommonsHttpOAuthProvider(Constants.REQUEST_URL, 
//     Constants.ACCESS_URL, Constants.AUTHORIZE_URL); 
//  } catch (Exception e) { 
//   Log.e(TAG, "Error creating consumer/provider", e); 
//  } 
// 
//  Log.i(TAG, "Starting task to retrieve request token."); 
//  new OAuthRequestTokenTask(this.getContext(), consumer, provider).execute(); 
// 
// } 

    private void setUpTitle() { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     mTitle = new TextView(getContext()); 
     mTitle.setText("Twitter"); 
     mTitle.setTextColor(Color.WHITE); 
     mTitle.setTypeface(Typeface.DEFAULT_BOLD); 
     mTitle.setBackgroundColor(0xFFbbd7e9); 
     mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN); 
     mTitle.setCompoundDrawablePadding(MARGIN + PADDING); 
     mContent.addView(mTitle); 
    } 

    public void setUpWebView(String u) { 
     mWebView = new WebView(getContext()); 

     mWebView.setVerticalScrollBarEnabled(false); 
     mWebView.setHorizontalScrollBarEnabled(false); 
     mWebView.loadUrl(u); 

     mWebView.setWebViewClient(new TwitterWebViewClient()); 
     mWebView.getSettings().setJavaScriptEnabled(true); 
     mWebView.setLayoutParams(FILL); 

     mContent.addView(mWebView); 
    } 

    private class TwitterWebViewClient extends WebViewClient { 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 

     @Override 
     public void onReceivedError(WebView view, int errorCode, 
       String description, String failingUrl) { 
      super.onReceivedError(view, errorCode, description, failingUrl); 
      /// mListener.onError(description); 
      TwitterDialog.this.dismiss(); 
     } 

     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      super.onPageStarted(view, url, favicon); 
      mSpinner.show(); 
      progressDialogRunning = true; 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      super.onPageFinished(view, url); 
      String title = mWebView.getTitle(); 
      if (title != null && title.length() > 0) { 
       mTitle.setText(title); 
      } 
      progressDialogRunning = false; 
      mSpinner.dismiss(); 
     } 

    } 

    @Override 
    protected void onStop() { 
     progressDialogRunning = false; 
     super.onStop(); 
    } 

    public void onBackPressed() { 
     if(!progressDialogRunning){ 
      TwitterDialog.this.dismiss(); 
     } 
    } 
} 

的logcat的輸出

05-02 12:47:17.517: E/com.noqwerty.m.twitter.OAuthRequestTokenTask(1703): Error during OAUth retrieve request token 
05-02 12:47:17.517: E/com.noqwerty.m.twitter.OAuthRequestTokenTask(1703): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
05-02 12:47:17.517: E/com.noqwerty.m.twitter.OAuthRequestTokenTask(1703): at android.os.Handler.<init>(Handler.java:197) 
05-02 12:47:17.517: E/com.noqwerty.m.twitter.OAuthRequestTokenTask(1703): at android.os.Handler.<init>(Handler.java:111) 
05-02 12:47:17.517: E/com.noqwerty.m.twitter.OAuthRequestTokenTask(1703): at android.webkit.WebViewClassic$PrivateHandler.<init>(WebViewClassic.java:7242) 
+0

請給你的logcat錯誤日誌 – 2013-05-02 13:40:41

+0

十分鐘前問完全相同的問題。例外基本上說這一切。查看它命名的類的文檔可以告訴你所需要知道的一切。 – 2013-05-02 13:40:47

+0

@ skyblueandroid007我編輯了它並添加了logcat – 2013-05-02 13:47:48

回答

0

看來,你的一些方法的嘗試從後臺訪問UI 線程並失敗。將用戶界面邏輯移至onPostExecute,以在UI線程上處理 。