【问题标题】:findViewById problems for webview in navigation drawer导航抽屉中 webview 的 findViewById 问题
【发布时间】:2015-04-22 15:56:22
【问题描述】:

我在使用 setcontentview 和 findviewbyid 时遇到了问题。当用户使用导航抽屉单击黑板按钮时,我正在尝试拉出网页。

片段.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"
/>

过去几天我一直坚持这一点。 提前谢谢!!!

-大卫

【问题讨论】:

    标签: java android xml android-fragments webview


    【解决方案1】:

    您的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"
    />
    

    此方法必须返回 Void!不查看:

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

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多