简体   繁体   English

导航抽屉中WebView的findViewById问题

[英]findViewById problems for webview in navigation drawer

I'm getting something wrong with setcontentview & findviewbyid. 我在setcontentview和findviewbyid方面遇到了问题。 I'm trying to pull up a webpage when the user, using the navigation drawer, clicks on the blackboard button. 当用户使用导航抽屉单击黑板按钮时,我试图拉出一个网页。

fragment.java 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;
}
}

and the xml file: 和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"
/>

I have been stuck on this for the past couple of days. 在过去的几天里,我一直坚持这一点。 Thanks in advance!!! 提前致谢!!!

-David -大卫

your fragment_blackboard.xml must contain your Webview , remember you are inside a Fragment : 您的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"
/>

This method must return Void! 此方法必须返回Void! not View: 不查看:

@Override
public View onCreate( Bundle savedInstanceState){

Your code must looks like: 您的代码必须类似于:

@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);
}

and your layout: 和您的布局:

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM