1

URLをロードするWeViewを含むアクティビティがあり、サイトのロード中に小さな待機中のダイアログを表示したいので、これを試しました:

private ProgressDialog dialog = new ProgressDialog(MyNameActivity.this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
        setContentView(R.layout.web_view_activity);

        WebView wv;
        wv = (WebView) findViewById(R.id.areaWebSolver);
        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
            @Override
        public void onPageFinished(WebView view, String url) {                  
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }

        });
        dialog.setMessage("Loading..Please wait.");
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        wv.loadUrl(url);

        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true);

    }

残念ながら機能せず、アプリがクラッシュしsource not foundます...進行状況ダイアログコードを削除しようとすると、アクティビティが機能します。どうしたの?どうすればこれを修正できますか?

4

4 に答える 4

0

ThreadPolicyメソッドの後に何らかの理由でを設定できsetContentView()ます。進行状況ダイアログは取得されません。Contextこれは、以下のステートメントがアクティビティコンテキストをまったくロードしないことです。

private ProgressDialog dialog = new ProgressDialog(this);

私のために働く以下の疑似コードを見つけてください。

private ProgressDialog dialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.web_view_activity);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

    dialog = new ProgressDialog(this);
    dialog.setMessage("Loading..Please wait.");
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();

    WebView wv = (WebView) findViewById(R.id.areaWebSolver);
    wv.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
        @Override
        public void onPageFinished(WebView view, String url) {                  
           if (dialog.isShowing()) {
              dialog.dismiss();
           }
        }
        ...
    }

ソースが見つからないのは、リンクした他の外部ライブラリが原因で、IDE のクラスパスで利用できない可能性があります。詳細については、こちらをお読みください。

于 2013-07-01T04:12:41.810 に答える
0

これ

     private ProgressDialog dialog = new ProgressDialog(MyNameActivity.this);
     //This will give you NullPointerException. 
     // You can use the activity context after you set the content to the activity.    

する必要があります

     private ProgressDialog dialog;   
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    setContentView(R.layout.web_view_activity); 
    dialog = new ProgressDialog(MyNameActivity.this);
    .... 
    }
于 2013-07-01T04:42:09.830 に答える
0

Webviewアクティビティでプログレスバーを使用する必要があります

// Just find 
 progressBar = (ProgressBar) findViewById(R.id.progressBar1);
//in onCreate Method of activity and than after 

    @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
                progressBar.setVisibility(View.GONE);
            }
      //Progress bar gone in onFinished method of webview.

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <WebView
        android:id="@+id/webview01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </WebView>

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>
于 2013-07-01T05:23:57.270 に答える