导航抽屉中 webview 的 findViewById 问题

findViewById problems for webview in navigation drawer

setcontentview 和 findviewbyid 出现问题。当用户使用导航抽屉单击黑板按钮时,我试图调出一个网页。

fragment.java

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 BlackBoardFragment extends Fragment {

public BlackBoardFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_blackboard, container, false);
    return rootView;
}


@Override
public View onCreate( Bundle savedInstanceState){

    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.loadUrl("http://www.example.com");
    return myWebView;
}
}

和 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

这几天一直卡在这个问题上。 提前致谢!!!

-大卫

您的 fragment_blackboard.xml 必须包含您的 Webview,请记住您在 Fragment 内:

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

此方法必须return作废!未查看:

@Override
public View onCreate( Bundle savedInstanceState){

您的代码必须如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_blackboard, container, false);
  WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
    myWebView.loadUrl("http://www.example.com");
    return rootView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

和您的布局:

<?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/webview"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"/>

</RelativeLayout>