0
我想實現WebView的方式應該在一個Activity中實例化(同時加載html),它不應該在第一頁顯示,並且只有webview在另一個活動中可見。我曾嘗試以下方法,但無法實現它,以下是我的MainActivity啓用/禁用WebView在另一個活動
public class MainActivity extends Activity {
private WebView webView = null;
private Button startAppBtn = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createWebView();
startAppBtn = (Button) findViewById(R.id.button1);
startAppBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, SecondActivity.class);
Bundle bundle = new Bundle();
intent.putExtra("state",bundle);
webView.saveState(bundle);
startActivity(intent);
}
});
}
public void createWebView(){
setWebView((WebView) findViewById(R.id.webview1));
getWebView().getSettings().setJavaScriptEnabled(true);
getWebView().getSettings().setDatabaseEnabled(true);
getWebView().getSettings().setLoadWithOverviewMode(true);
getWebView().setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
getWebView().setScrollbarFadingEnabled(false);
getWebView().loadUrl("file:///android_asset/My.html");
getWebView().setVisibility(View.GONE);
}
public WebView getWebView() {
return webView;
}
public void setWebView(WebView webView) {
this.webView = webView;
}
}
和下面是SecondActivity,
public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
Intent intent = getIntent();
WebView webview = (WebView) findViewById(R.id.webview1);//at this line I am getting exception, I think because webview is in activity_main layout.
webview.restoreState((Bundle) intent.getParcelableExtra("state"));
webview.getSettings().setJavaScriptEnabled(true);
webview.setVisibility(View.VISIBLE);
}
}
我是新來的android的世界,努力就可以了請任何人能幫助我解決這個問題。