我在fragment_main.xml中定义了webview。是否有任何示例代码可以在onCreateView中加载Url?我的代码如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Banana", "inside onCreate");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
// Find that webView1
WebView mypointer = (WebView) rootView.findViewById(R.id.webView1);
// Open asset/index.html
if (mypointer != null) {
mypointer.loadUrl("file://android_asset/index.html");
} else {
Log.v("Banana", "my pointer is null");
}
return rootView;
}
然而,eclipse抱怨第二次覆盖,当我删除它时,它会运行,但不显示网址。指针返回null。
my fragment_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="apps101.javabook.MainActivity$PlaceholderFragment" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
如何在应用中加载网址?感谢。
更新:我发现了问题。我试图复制onCreateView。删除副本,现在它似乎工作正常(是的,也忘了斜线)。问题解决了。感谢。
答案 0 :(得分:3)
使用此,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
// Find that webView1
WebView mypointer = (WebView) rootView.findViewById(R.id.webView1);
// Open asset/index.html
if (mypointer != null) {
mypointer.loadUrl("file:///android_asset/index.html");
} else {
Log.v("Banana", "my pointer is null");
}
return rootView;
}
你错过了'/'文件:///