0

我在 shouldoverrideurlloading 方法中遇到了麻烦,其中没有显示被覆盖的 URL。相反,出现了一个空白屏幕,并且 Logcat 没有显示任何错误。我在下面提供了代码。我在哪里犯错了?请指教

package com.example.webviewkm;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.View;
import android.webkit.HttpAuthHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.net.http.SslError;
import android.webkit.SslErrorHandler;

public class MainActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView webView = (WebView) findViewById(R.id.MyWebView);
    webView.setInitialScale(67);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webView.setVerticalScrollBarEnabled(false);
    webView.setHorizontalScrollBarEnabled(false);

    webView.setWebViewClient(new WebViewClient() {

        @Override 
        public void onReceivedLoginRequest(WebView view, String realm, 
                        String account, String args) { 


                System.err.println(realm); 
                System.err.println(account); 
                System.out.println(args); }

        @Override
        public void onReceivedError( WebView view, int errorCode, String description, String failingUrl ) {
            Toast.makeText(view.getContext(), "Unknown Error", Toast.LENGTH_LONG).show();
            System.err.println(errorCode + " - " + description + "-" + failingUrl); 


        }
         @Override
            public void onReceivedSslError( WebView view, SslErrorHandler handler, SslError error ) {
              System.err.println("SSL ERROR");  

             handler.proceed();
            }
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        intent.putExtra("url", url);
        startActivity(intent);
        return true;
      }

      @Override
      public void onReceivedHttpAuthRequest(WebView view, final HttpAuthHandler handler, final String host,
          String realm) {
          System.err.println("HTTP auth request"); 
        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);        


        if (handler.useHttpAuthUsernamePassword()) {
          if (preferences.contains("username")) {
            handler.proceed(preferences.getString("username", null), preferences.getString("password", null));
            return;
          }

        }

        new Dialog(MainActivity.this){
          @Override
          protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setTitle(host);
            setContentView(R.layout.credentials);


            final EditText userName = (EditText) findViewById(R.id.UserName);
            final EditText password = (EditText) findViewById(R.id.Password);
            userName.setText(preferences.getString("username", ""));
            password.setText(preferences.getString("password", ""));

            Button button = (Button) findViewById(R.id.GoButton);
            button.setOnClickListener(new Button.OnClickListener(){

              public void onClick(View v) {
                String userName2 = userName.getText().toString();
                String password2 = password.getText().toString();
                Editor edit = preferences.edit();
                edit.putString("username", userName2);
                edit.putString("password", password2);
                edit.commit();
                handler.proceed(userName2, password2);
                dismiss();
              }});
          }

        }.show();
      }
    });

    String url;
    if (getIntent().hasExtra("url")) {
      url = getIntent().getStringExtra("url");
    } else {
     url = "https://kee.mahindrasatyam.com/_layouts/mobile/mobilesearch.aspx";


    }
    webView.loadUrl(url);

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    Intent intent = new Intent(MainActivity.this, MainActivity.class);
    startActivity(intent);
    return true;
  }

}
4

1 回答 1

5

在您粘贴在下面方法 shouldOverrideUrlLoading 中的代码中,

public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        intent.putExtra("url", url);
        startActivity(intent);
        return true;
      }

您只是在启动相同的活动,并且将 url 作为意图额外传递,这不会在 webview 中加载 url,也不会给您一个错误,如果您打算在 webview 中加载 url,请使用

webView.loadUrl(url)在 shouldOverrideUrlLoading 方法中。

于 2013-08-17T18:41:22.950 回答