我怎样才能在这里添加一个webview?

时间:2015-03-14 14:14:12

标签: java android xml eclipse webview

我应该在这里做什么工作我的脚本,我已经尝试了一切,但没有任何作用..他给出了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;
    }

}

2 个答案:

答案 0 :(得分:0)

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

}

P.S。从导入中取消import com.dehvb.deheerenveenseboys.R;,可能会导致一些问题。

答案 1 :(得分:0)

如果你的布局文件中已经有了webview而不是直接使用它!

WebView wb = (WebView)rootView.findViewById(R.id.webView);
wb.loadUrl(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;
}
}

我测试了它,如果你愿意,我可以发布截图!