2013-08-17 45 views
-1

我在shouldoverrideurlloading方法中遇到麻煩,其中被覆蓋的URL沒有被顯示。相反,一個空白的白色屏幕即將出現,Logcat顯示沒有錯誤。我在下面提供了代碼。我犯了什麼錯誤?請指教android中的shouldoverrideurlloading方法

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; 
    } 

} 

回答

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作爲意圖額外費用,這將不會加載該URL中webview既不會給你一個錯誤,並且如果你打算在webview中加載url,使用

webView.loadUrl(url) in shouldOverrideUrlLoading方法。

+1

@Nargis ...許多許多THANKs你...這解決了我的問題... – user2685516

相關問題