【问题标题】:How can i add here a webview?我怎样才能在这里添加一个网页视图?
【发布时间】:2015-03-14 14:14:11
【问题描述】:

我应该怎么做我的脚本,我已经尝试了一切,但没有任何工作..他给 webview 一个错误?

package com.dehvb.dehvbapp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;


public class ProgrammaFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_programma, container, false);

        WebView myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://www.example.com");

        return rootView;
    }

}

【问题讨论】:

    标签: java android xml eclipse webview


    【解决方案1】:
    public class ProgrammaFragment extends Fragment {
        Webview wv;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
            View rootView = inflater.inflate(R.layout.fragment_programma, container, false);
            wv = (WebView) rootView.findViewById(R.id.yourWebviewId);
    
            //Use the thread policy only if your load URL doesn't work on main thread.
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
    
            wv.loadUrl("http://www.example.com");
    
            return rootView;
        }
    
    }
    

    附:从导入中删除import com.dehvb.deheerenveenseboys.R;,这可能会导致一些问题。

    【讨论】:

    • 他通过 ""wv = (WebView) rootView.findViewById(R.id.Webview1);"" 我应该添加这两个中的一个 "" create constant 'webview1' in type ' id' "" or "" 在类型 'id' 中创建字段 'webview1' ??
    • 在你的 fragment_programma 中,你需要有一个 id="@+id/webview1" 的 webview 并且在你的类中: wv = (WebView) rootView.findViewById(R.id.webview1);跨度>
    • 我已经做到了,你可能会找我哪里出了问题?我可以帮你上传吗?
    • 更新你的答案,发布你的 xml 文件和你的 logcat
    • 我找不到我的logcat,所以这里是脚本drive.google.com/…
    【解决方案2】:

    如果您的布局文件中已经有 webview,则直接使用!

    WebView wb = (WebView)rootView.findViewById(R.id.webView);
    wb.loadUrl(url);
    

    将 url 替换为您的网址,例如“https://stackoverflow.com/”。 希望这有帮助!

    编辑: 首先像这样更新您的清单:

        <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.dehvb.dehvbapp"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="11"
            android:targetSdkVersion="17" />
    
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.dehvb.dehvbapp.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    和你的 ProgrammaFragment.class

    public class ProgrammaFragment extends Fragment
    {
    WebView wv;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    
        View rootView = inflater.inflate(R.layout.fragment_programma, container, false);
        wv = (WebView) rootView.findViewById(R.id.WebView1);
    
        //Use the thread policy only if your load URL doesn't work on main thread.
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    
        wv.loadUrl("https://stackoverflow.com/");
    
        return rootView;
    }
    }
    

    我已经测试过了,如果你愿意,我可以发截图!

    【讨论】:

    • 所以用 ThreadPolicy 检查我的解决方案,我认为可能是这样!
    • 下载此代码并重试:Demo
    • 我不让它工作,你可能会找我我做错了什么?然后我发布我的文件
    • 这些是我对您在 drive.google.com 中提供的代码所做的唯一更改...。请花点时间看看有哪些更改,您可以将这些内容替换为项目中的相关文件并检查!
    猜你喜欢
    • 1970-01-01
    • 2010-11-03
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多