2012-04-25 145 views
2

我在我的主佈局上設置了一個按鈕,該按鈕應該執行在另一個佈局上設置的webview的goBack方法。 setOnClickListener for backButton時,應用程序崩潰。我做錯了什麼?謝謝。android webview後退按鈕setOnClickListener

web視圖類,

public class webPush extends Activity { 

     WebView webview; 
     Button backButton; 

     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.webview); 

      webview = (WebView) findViewById(R.id.webview1); 

      backButton = (Button)findViewById(R.id.button1); 

      backButton.setOnClickListener(new OnClickListener() 
      { 
        @Override 
        public void onClick(View arg0) 
        { 
         webview.goBack(); 
        } 
      }); 

      webview.getSettings().setJavaScriptEnabled(true);  

      webview.setWebViewClient(new WebViewClient()); 

      webview.loadUrl("http://www.google.com"); 

     } 

從主要佈局,

<Button 
       android:id="@+id/button1" 
       style="?android:attr/buttonStyleSmall" 
       android:layout_width="78dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.02" 
       android:text="Back" /> 
+0

什麼是崩潰報告?還有,你需要確保有一個頁面回到 – thepoosh 2012-04-25 11:24:14

+0

說不要太多...無法啓動活動 – Jaume 2012-04-25 11:31:37

+0

請發佈所有的報告,也許我們是能夠看到你錯過的東西 – thepoosh 2012-04-25 11:39:57

回答

0

你在哪個佈局中放置了按鈕?它是在main.xml或webview佈局? 如果按鈕位於main.xml中,則setContentView(R.layout.main);

+0

你得到按鈕的空指針例外。我的問題是你有按鈕id在webview佈局button1按鈕? – sush 2012-04-25 11:48:01

+0

??按鈕放在主佈局上,webview放置在webview佈局上。按鈕應該會收到一個動作並使webview返回。 – Jaume 2012-04-25 11:49:32

+0

不,按鈕已設置button1編號,但在主佈局 – Jaume 2012-04-25 11:51:28

3

我覺得你有空指針異常....因爲... webview is null

,就把這行的setContentView後。 ..

webview = (WebView) findViewById(R.id.webview1); 
+0

毫米肯定,試過但同樣的問題。我編輯了我的問題。 – Jaume 2012-04-25 11:29:42

+0

在這裏添加您的完整logcat ... – 2012-04-25 11:32:13

+0

我不知道如何附上完整的日誌文件。如果這行不夠,請指教,E/AndroidRuntime(374):引起:java.lang.RuntimeException:無法啓動活動ComponentInfo {com.nb.TabBar/com.nb.TabBar.webPush}:java.lang。 NullPointerException – Jaume 2012-04-25 11:41:49

0

試試這個添加:

if (ourBrowser.canGoBack()) { 
      ourBrowser.goBack(); 
} 
+0

setOnClickListener爲按鈕對象時崩潰。不是一個解決方案 – Jaume 2012-04-25 11:33:20