0

Webビューコンポーネントを含むダイアログからtwitter APIを使用してAndroidアプリケーションにログインしています

このエラーは、Logcat の「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)
4

1 に答える 1

0

一部のメソッドがバックグラウンド スレッドから UI にアクセスしようとして失敗したようです。UI ロジックを onPostExecute に移動して、UI スレッドで処理します。

于 2013-05-02T14:19:45.840 に答える