这是我第一次使用WebView小部件,我已经通过Google和StackExchange进行了搜索,无法找到问题的解决方案。
我在FragmentActivity上放置了一个WebView,由一个主Activity加载,无论我使用“http://www.google.com”作为URL还是指定一个本地.html文件,它都是空白的(非常简单)一个包含几个段落,标题和链接)。它未在下面显示,但我的AndroidManifest.xml具有INTERNET权限(和EXTERNAL_STORAGE)。
activity_main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:name="com.adamtcarlson.websiteviewer.MainActivityFragment"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<FrameLayout
android:id="@+id/frm_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java:
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
public class MainActivity extends FragmentActivity {
/**
* The Activity's tag.
*/
private static final String Tag = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v(Tag, "onCreate()");
populateContentFragment(false);
}
private void populateContentFragment(boolean addToBackStack) {
Log.v(Tag, "populateContentFragment()");
Uri uri = Uri.parse("http://www.google.com/");
//Uri uri = Uri.parse("file:///data/app/testpage.html");
Fragment fragment = WebBrowserFragment.newInstance(uri);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()
.replace(R.id.frm_content, fragment);
if (addToBackStack) {
fragmentTransaction.addToBackStack(Tag);
}
fragmentTransaction.commit();
}
}
fragment_webbrowser.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="#AAAAAA">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/wbvBrowser">
</WebView>
</RelativeLayout>
WebBrowserFragment.java:
import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* A placeholder fragment containing a simple view.
*/
public class WebBrowserFragment extends Fragment {
private WebView wbvBrowser = null;
private String browserUri = null;
/**
* The Activity's tag.
*/
private static final String Tag = "WebBrowserFragment";
public WebBrowserFragment() {
Bundle args = getArguments();
if (args != null) {
browserUri = args.getString("Uri");
}
}
public static WebBrowserFragment newInstance(Uri uri) {
Log.v(Tag, "newInstance(). Uri: " + uri.toString());
WebBrowserFragment fragment;
if (uri != null) {
fragment = new WebBrowserFragment();
Bundle args = new Bundle();
args.putString("Uri", uri.toString());
fragment.setArguments(args);
}
else {
fragment = new WebBrowserFragment();
}
return fragment;
}
@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.v(Tag, "onCreateView()");
View rootView = inflater.inflate(R.layout.fragment_webbrowser, container, false);
wbvBrowser = (WebView) rootView.findViewById(R.id.wbvBrowser);
wbvBrowser.getSettings().setJavaScriptEnabled(true);
wbvBrowser.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
wbvBrowser.loadUrl(browserUri);
return rootView;
}
}
我应该在片段中注意到我尝试评论.setWebViewClient()
电话,但这也没有做任何事情。我从一本书中获得了原始代码,“Android编程:大书呆子牧场指南”。我看不到任何错误或任何看起来不合适的logcat日志。我已经尝试通过模拟器查看应用程序,使用API 10和19.我也尝试在我的LG Ultimate 2上安装.apk并且都显示空白屏幕(我将背景更改为片段的RelativeLayout为灰色以确保WebView出现了。)
非常感谢您的帮助。
答案 0 :(得分:1)
wbvBrowser.loadUrl(browserUri);
browserUri
未设置
只需在该行前添加browserUri = getArguments().getString("Uri");
。
即
browserUri = getArguments().getString("Uri");
wbvBrowser.loadUrl(browserUri);