无法从android_资源加载url,返回空指针
我在fragment_main.xml中定义了一个webview。是否有在onCreateView中加载Url的示例代码?我的代码如下所示:无法从android_资源加载url,返回空指针,android,xml,Android,Xml,我在fragment_main.xml中定义了一个webview。是否有在onCreateView中加载Url的示例代码?我的代码如下所示: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i("Banana", "inside
@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抱怨第二个覆盖,当我删除它时,它会运行,但不会显示url。指针返回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>
如何在应用程序中加载URL?谢谢
更新:我发现了问题。我试图复制onCreateView。删除了副本,现在似乎工作正常(是的,也忘记了斜杠)。问题解决了。谢谢。用这个
@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;
}
您遗漏了一个“/”文件:///使用此文件
@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;
}
您遗漏了一个“/”文件:///使用此文件
@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;
}
您遗漏了一个“/”文件:///使用此文件
@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;
}
您遗漏了一个“/”它是文件://查看后,您应该使用三个正斜杠(即
file:///etc...
)而不是两个。希望帮助在查看之后,您应该使用三个正斜杠(即file:///etc...
)而不是两个。希望帮助在查看之后,您应该使用三个正斜杠(即file:///etc...
)而不是两个。希望帮助在查看之后,您应该使用三个正斜杠(即file:///etc...
)而不是两个。希望你已经错过了一个“/”的帮助file:///Mention 答案正文中缺少的正斜杠你漏掉了一个“/”它是file:///Mention 答案正文中缺少的正斜杠你漏掉了一个“/”它是file:///Mention 答案正文中缺少的正斜杠你漏掉了一个“/”它是file:///Mention 答案正文中缺少的正斜杠