我已经获得了滑动菜单的源代码,并且希望在其中实现webview,但它似乎无法工作,并且无法找到关于如何执行此操作的明确答案。这是我的代码,希望它对我想要实现的目标有意义:
import info.androidhive.slidingmenu.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
public class TestingPage extends Fragment {
public TestingPage(){} //Have removed this code, but still gives same error
private WebView webView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_testpage, container);
webView = (WebView) findViewById(R.id.Webview1);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl("file:///android_asset/index.html");
return rootView;
}
}
现在我遇到的问题是findViewById我有一个错误选项:"创建方法findViewById(int)
当我创建这个(通过给出的选项)时,我得到没有错误然后这是我得到的:
private WebView findViewById(int webview1) {
// TODO Auto-generated method stub
return null;
}
}
现在我不知道该怎么做?当我运行它并测试应用程序时,我得到了这个:
09-11 13:39:07.649: E/AndroidRuntime(31488): FATAL EXCEPTION: main
09-11 13:39:07.649: E/AndroidRuntime(31488): java.lang.NullPointerException
09-11 13:39:07.649: E/AndroidRuntime(31488): at com.test.testapp.findViewById(TestingPage.java:33)
09-11 13:39:07.649: E/AndroidRuntime(31488): at com.test.testapp.onCreateView(TestingPage.java:22)
09-11 13:39:07.649: E/AndroidRuntime(31488): at android.app.Fragment.performCreateView(Fragment.java:1699)
09-11 13:39:07.649: E/AndroidRuntime(31488): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
09-11 13:39:07.649: E/AndroidRuntime(31488): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
09-11 13:47:58.229: E/AndroidRuntime(32545): FATAL EXCEPTION: main 09-11 13:47:58.229: E/AndroidRuntime(32545): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first
被修改
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/Webview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
使用正确的代码编辑
问题在于此代码:
View rootView = inflater.inflate(R.layout.fragment_campsites, container);
我是如何解决这个问题的,最后是加错:
View rootView = inflater.inflate(R.layout.fragment_campsites, container, false);
现在一切正常。
答案 0 :(得分:1)
用此代码替换您的代码
View rootView = inflater.inflate(R.layout.fragment_testpage, container);
webView = (WebView) rootView.findViewById(R.id.Webview1);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl("file:///android_asset/index.html");
只需更改webView = (WebView) rootView.findViewById(R.id.Webview1);
行
答案 1 :(得分:0)
你是rootview中的webview,并且id在根视图中
View rootView = inflater.inflate(R.layout.fragment_testpage, container);
webView = (WebView) rootView .findViewById(R.id.Webview1);
答案 2 :(得分:0)
变化
View rootView = inflater.inflate(R.layout.fragment_testpage, container);
到
View rootView = inflater.inflate(R.layout.fragment_testpage, container,false);
inflate方法中缺少“false”参数会导致将“rootView”添加到“容器”中。 在onCreateView方法
中返回“rootView”时会导致异常