2012-04-25 163 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