您好我正在为 Youtube 频道制作一个小型Android应用,我有一个标签主机,其中包含两个不同的活动,这两个活动都在创建时运行Web视图
我有一个Web视图客户端处理外部链接(每个Web视图活动一个)。
第一个标签加载网页正常,但是当我从标签切换到它时,第二个标签只是空白我如何解决这个问题?
以下是处理标签的主要活动的正文代码。
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
TabSpec youtubespec = tabHost.newTabSpec("Youtube");
// setting Title and Icon for the Tab
youtubespec.setIndicator("Youtube", getResources().getDrawable(R.drawable.youtube_icon));
Intent youtubeIntent = new Intent(this, Youtube.class);
youtubespec.setContent(youtubeIntent);
TabSpec websitespec = tabHost.newTabSpec("Website");
// setting Title and Icon for the Tab
websitespec.setIndicator("Website", getResources().getDrawable(R.drawable.website_icon));
Intent websiteIntent = new Intent(this, Website.class);
websitespec.setContent(websiteIntent);
tabHost.addTab(youtubespec);
tabHost.addTab(websitespec);
}
以下是第一个网络视图的代码。 WebView web1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.youtube);
web1 = (WebView) findViewById(R.id.webyt);
web1.setWebViewClient(new WebViewClient1());
WebSettings webSettings = web1.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setPluginState(PluginState.ON);
webSettings.setJavaScriptCanOpenWindowsAutomatically(false);
webSettings.setSupportMultipleWindows(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
web1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
web1.loadUrl("http://www.youtube.com/user/theanykeyyt");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
web1.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
以下是第二个网络视图的代码
WebView web2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.website);
web2 = (WebView) findViewById(R.id.websitea);
web2.setWebViewClient(new WebViewClient2());
WebSettings webSettings = web2.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setPluginState(PluginState.ON);
webSettings.setJavaScriptCanOpenWindowsAutomatically(false);
webSettings.setSupportMultipleWindows(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
web2.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
web2.loadUrl("http://theanykeyyt.wix.com/theanykey");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && web2.canGoBack()) {
web2.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
这是网络客户端
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
答案 0 :(得分:0)
修复只需要将其添加到清单
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>