从其他标签返回时,最后一个链接未打开 并且onPause()不起作用。它显示空指针异常,并且应用程序会自动关闭,只需单击除第一个选项卡之外的其他选项卡。 片段标签代码
public class FragmentTab extends Fragment {
protected WebView myWebView;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
WebView myWebView = (WebView) v.findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
if(savedInstanceState != null)
myWebView.restoreState(savedInstanceState);
else
myWebView.loadUrl("http://www.example.com");
return v;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
myWebView.saveState(outState);
}
}
以下是MainActivity
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator(null,ContextCompat.getDrawable(this,R.drawable.home)),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator(null,ContextCompat.getDrawable(this,R.drawable.deals_offers)),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab3").setIndicator(null,ContextCompat.getDrawable(this,R.drawable.profile)),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab4").setIndicator(null, ContextCompat.getDrawable(this, R.drawable.menu)),
FragmentTab.class, null);
}
}
答案 0 :(得分:3)
使用此库:https://github.com/frankiesardo/icepick
从WebView中删除protected
@State
WebView myWebView; // This will be automatically saved and restored
在您的活动方法中添加以下行将休息处理。
在OnCreate方法
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Icepick.restoreInstanceState(this, savedInstanceState);
}
在onSaveInstanceState
@Override public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
Icepick.saveInstanceState(this, outState);
}
答案 1 :(得分:2)
在代码中看起来myWebView始终为null:
public class FragmentTab extends Fragment {
protected WebView myWebView;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
myWebView = (WebView) v.findViewById(R.id.webview);
// instead of WebView myWebView = (WebView) v.findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
if(savedInstanceState != null)
myWebView.restoreState(savedInstanceState);
else
myWebView.loadUrl("http://www.example.com");
return v;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
myWebView.saveState(outState);
}
}
答案 2 :(得分:1)
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
myWebView.saveState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle outState) {
super.onRestoreInstanceState(outState);
myWebView.restoreState(outState);
}
根据源代码。
/**
* Restores the state of this WebView from the given Bundle. This method is
* intended for use in {@link android.app.Activity#onRestoreInstanceState}
* and should be called to restore the state of this WebView. If
* it is called after this WebView has had a chance to build state (load
* pages, create a back/forward list, etc.) there may be undesirable
* side-effects. Please note that this method no longer restores the
* display data for this WebView.
*
* @param inState the incoming Bundle of state
* @return the restored back/forward list or null if restoreState failed
*/
public WebBackForwardList restoreState(Bundle inState) {
checkThread();
return mProvider.restoreState(inState);
}