2013-10-08 53 views
2

我正在創建一個帶有3個ActionBar選項卡的Web應用程序。每個選項卡都會觸發一個webview。在其中一個選項卡上,我需要Android設備後退按鈕來觸發該webview的goBack操作。我正在嘗試一種類似於How to add "Go Back" function in WebView inside Fragment?的blackbelt和netinept建議的解決方案,但是我在MainActivity.java文件中收到以下錯誤 - 「方法canGoBack()未定義爲類型FragmentTab3」。Android webview canBack()錯誤與ActionBarSherlock片段

這是我的代碼。請讓我知道我可能做錯了什麼。

--- MainActivity.java ---

在那裏我得到 「的方法canGoBack()是未定義的類型FragmentTab3」

public class MainActivity extends SherlockFragmentActivity { 

    ActionBar.Tab Tab1,Tab2,Tab3; 
    Fragment fragmentTab1 = new FragmentTab1(); 
    Fragment fragmentTab2 = new FragmentTab2(); 
    Fragment fragmentTab3 = new FragmentTab3(); 


    @Override protected void onSaveInstanceState(Bundle outState){ 
     super.onSaveInstanceState(outState); 
     int index = getSupportActionBar().getSelectedNavigationIndex(); 
     outState.putInt("selected_tab_index", index); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ActionBar actionBar = getSupportActionBar(); 

     // Hide Actionbar Icon 
     actionBar.setDisplayShowHomeEnabled(false); 

     // Hide Actionbar Title 
     actionBar.setDisplayShowTitleEnabled(false); 

     // Create Actionbar Tabs 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Set Tab Icon and Titles 
     Tab1 = actionBar.newTab().setText("Tab 1"); 
     Tab2 = actionBar.newTab().setText("Tab 2"); 
     Tab3 = actionBar.newTab().setText("Tab 3"); 


     // Set Tab Listeners 
     Tab1.setTabListener(new TabListener(fragmentTab1)); 
     Tab2.setTabListener(new TabListener(fragmentTab2)); 
     Tab3.setTabListener(new TabListener(fragmentTab3)); 

     // Add tabs to actionbar 
     actionBar.addTab(Tab1); 
     actionBar.addTab(Tab2); 
     actionBar.addTab(Tab3); 

     if (savedInstanceState != null) { 
      int index = savedInstanceState.getInt("selected_tab_index", 0); 
      getSupportActionBar().setSelectedNavigationItem(index); 
     } 

    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
    } 

    @Override 
    public void onBackPressed() { 
      Fragment webview = getSupportFragmentManager().findFragmentById(R.id.webview); 
      if (webview instanceof FragmentTab3) { 
       boolean goback = ((FragmentTab3)webview).canGoBack(); 
       if (!goback) 
        super.onBackPressed(); 
      } 
    }  

} 

--- FragmentTab3.java-- -

public class FragmentTab3 extends SherlockFragment 
{ 

    public WebView webView; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) 
    { 
     View rootView = inflater.inflate(R.layout.fragmenttab3, container, false); 

     webView = (WebView) rootView.findViewById(R.id.webview); 
     webView.setBackgroundColor(0); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.setWebViewClient(new WebViewClient()); 
     webView.loadUrl("http://www.example.com"); 
     webView.requestFocusFromTouch(); 

     return rootView; 
    } 

} 

--- fragmenttab3.xml ---

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <WebView 
     android:id="@+id/webview" 
     android:tag="webview_tag" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 

</LinearLayout> 

回答

0

作爲erros說Fragment沒有叫做canGoBack的方法。你可以這樣做:

public class FragmentTab3 extends SherlockFragment 
{ 
    /// your code 

    public boolean canGoBack() { 
     return webView != null && webView.canGoBack(); 
    } 

} 
+0

謝謝。我添加了你建議的代碼,但它還沒有工作。我運行了調試器,看到webview在'Fragment webview = getSupportFragmentManager()'中是Null。findFragmentById(R.id.webview);' @blackbelt – user2859868

+0

仍然在做一些測試,但是我通過改變'Fragment webview = getSupportFragmentManager()。findFragmentById(R.id.webview);'來分割webview = getSupportFragmentManager()。findFragmentById(R.id。 fragment_container);' – user2859868

0

這對我在我的MainActivity.java工作我使用4個選項卡每個與Web視圖。

@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    int currPage = mPager.getCurrentItem(); 
    WebView wv = null; 
    switch (currPage) { 
    case 0: 
     wv = (WebView) mPager.getChildAt(currPage).findViewById(R.id.webView1); 
     break; 
    case 1: 
     wv = (WebView) mPager.getChildAt(currPage).findViewById(R.id.webView2); 
     break; 
    case 2: 
     wv = (WebView) mPager.getChildAt(currPage).findViewById(R.id.webView3); 
     break; 
    case 3: 
     wv = (WebView) mPager.getChildAt(currPage).findViewById(R.id.webView4); 
     break; 
    } 

    if (wv != null) { 
     if (wv.canGoBack()) { 
      wv.goBack(); 
     } else { 
      super.onBackPressed(); 
     } 
    } else { 
     super.onBackPressed(); 
    } 
}