¿如何使用本地html文件从这里加载webView?

时间:2014-10-24 11:09:34

标签: android android-intent android-fragments webview

我有这个,我希望从该按钮加载带有我在资产文件夹中的某些本地html文件的webview。

@Override
public void onResume() {
    super.onResume();

    mProductividadYDesarrolloPersonalButton = (Button)getView().findViewById(R.id.button);
    mProductividadYDesarrolloPersonalButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // What should I add here?

        }
    });

我该怎么办?

网络视图的活动是:

public class lectura_view extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        // Buscar AdView como recurso y cargar una solicitud.
        AdView adView = (AdView)this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        Intent launchingIntent = getIntent();
        String content = launchingIntent.getData().toString();

        WebView mWebView;

        mWebView = (WebView) findViewById(R.id.lectura_webView);
        mWebView.loadUrl(content);

        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setDisplayZoomControls(false);

        mWebView.setLongClickable(true);
        mWebView.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.web_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

编辑1:这是我尝试第一个解决方案时给出的错误。

            @Override
        public void onClick(View v) {

        WebView mWebView;

        mWebView = (WebView) getView().findViewById(R.id.lectura_webView);
        mWebView.loadUrl("\"file:///android_asset/productividad/GettingThingsDoneDavidAllen.html");

        }
    });

和日志

Log:10-24 13:47:46.740  22632-22632/com.estudiable.estudiable E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.estudiable.estudiable, PID: 22632
java.lang.NullPointerException
        at com.estudiable.estudiable.Seccion4$1.onClick(Seccion4.java:56)
        at android.view.View.performClick(View.java:4456)
        at android.view.View$PerformClick.run(View.java:18465)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

试试这个

wb=(WebView) findViewById(R.id.webView1);
    wb.setWebViewClient(new AppWebViewClients());
    wb.getSettings().setJavaScriptEnabled(true);
    wb.getSettings().setLoadWithOverviewMode(true);
    wb.getSettings().setUseWideViewPort(true);
    wb.getSettings().setBuiltInZoomControls(true);
    wb.getSettings().setPluginState(PluginState.ON);
    wb.getSettings().setAllowFileAccess(true);
    wb.loadUrl("file:///android_asset/yourfile.html");

答案 1 :(得分:0)

如果您将文件放入/assets/文件夹,则可以执行以下操作:

mWebView.loadURL("file:///android_asset/your_file.htm");

您可以在资产下创建其他目录,即:/assets/html/

mWebView.loadURL("file:///android_asset/html/your_file.htm");