2011-09-13 70 views
3

我正在使用以下代碼在我的Android應用中顯示webview。在webview中啓用後退按鈕

package com.company.myapp; 

import com.google.android.apps.analytics.GoogleAnalyticsTracker; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

public class ArticlesActivity extends Activity { 

    /** Initialize the Google Analytics Tracker */ 
    GoogleAnalyticsTracker tracker; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().requestFeature(Window.FEATURE_PROGRESS); 
     getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 
     WebView webview = new WebView(this); 
     setContentView(webview); 
     setProgressBarVisibility(true); 
     webview.getSettings().setJavaScriptEnabled(true); 
     webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
     final Activity activity = this; 
     tracker = GoogleAnalyticsTracker.getInstance(); 
     // Start the tracker, updating google every 20 seconds 
     tracker.start((String) getText(R.string.analyticsID), 20, this); 

     webview.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) { 
      activity.setProgress(progress * 100); 
      } 
     }); 

     webview.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     webview.loadUrl("http://www.google.com"); 
    } 
    @Override 
    public void onResume() { 
     tracker.trackPageView("ArticlesActivity"); 
     super.onResume(); 
    } 
    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     // Stop the tracker when it is no longer needed. 
     tracker.stop(); 
    } 
} 

我需要啓用後退按鈕如果歷史存在的,而不是剛出的WebView退一步。

I've tried many different code examples such as this但無法取得任何工作。當後退按鈕被按下時,應用程序纔會關閉。

這裏是我的代碼以後退按鈕的代碼,但它只是崩潰時則後退按鈕被按下的應用程序:

package com.company.myapp; 

import com.google.android.apps.analytics.GoogleAnalyticsTracker; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

public class ArticlesActivity extends Activity { 

    WebView webview; 

    /** Initialize the Google Analytics Tracker */ 
    GoogleAnalyticsTracker tracker; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().requestFeature(Window.FEATURE_PROGRESS); 
     getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 
     WebView webview = new WebView(this); 
     setContentView(webview); 
     setProgressBarVisibility(true); 
     webview.getSettings().setJavaScriptEnabled(true); 
     webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
     final Activity activity = this; 
     tracker = GoogleAnalyticsTracker.getInstance(); 
     // Start the tracker, updating google every 20 seconds 
     tracker.start((String) getText(R.string.analyticsID), 20, this); 

     webview.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) { 
      activity.setProgress(progress * 100); 
      } 
     }); 

     webview.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     webview.loadUrl("http://www.google.com"); 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 

      webview.goBack(); 

      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

    @Override 
    public void onResume() { 
     tracker.trackPageView("ArticlesActivity"); 
     super.onResume(); 
    } 
    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     // Stop the tracker when it is no longer needed. 
     tracker.stop(); 
    } 
} 

有人能幫助我的解決方案?

+0

取代它雖然你提到你試圖在鏈接中提到的例子,你也覆蓋onBackPressed方法? –

+0

感謝您的評論Rahul。我編輯了我的問題,並添加了我正在使用的示例中的代碼。實際的onKeyDown方法可以工作,因爲我可以在按下後退按鈕時顯示吐司,但是當我按下後退按鈕時使用應用程序崩潰之上的代碼時。我嘗試了onBackPressed,但同樣的事情發生。 – Brigante

+0

你說應用程序崩潰,你也可以發佈你的崩潰轉儲,日誌貓.. –

回答

7

Got it!你在這行

WebView webview = new WebView(this); 

而不是使用你的成員變量要創建內部函數變量的問題,因此你的成員變量的onkeydown函數內部空。

只是

webview = new WebView(this); 
+0

這樣做了!非常感謝你的幫助!! – Brigante