从SD卡播放webview中的视频时获取空指针异常

时间:2013-03-20 07:50:52

标签: android html video webview

在Html文件中,始终在下面的行中显示错误

<video src="file:///sdcard/kummar.m4v" width="300" onclick="playvideo('file:///sdcard/ku<p>testing of video element.</p>

这是我的Java代码。在这里,我已将视频文件保存在SD卡上,并将其命名为“kummar.m4v”,下面是我的HTML文件。我在资产中保存了Html文件 - &gt;新建 - &GT;文件 - &GT;的test.html。

当我调试代码时, 我发现了错误;在这一行,它抛出空指针异常

 webView.getSettings().setPluginState(WebSettings.PluginState.ON);

完整Java代码:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView webView = (WebView) findViewById(R.id.webView1);
    String html = "<embed src=\"file:///android_asset/" + "test.html" + " \"play=\"true\" loop=\"true\" width=\"100%\" height=\"100%\"> <embed>";
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", null);
    WebViewClient webViewClient = new WebViewClient();
    webView.setWebViewClient(webViewClient);
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            if (url.endsWith(".m4v")) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                intent.setDataAndType(Uri.parse(url), "video/*");
                view.getContext().startActivity(intent);
            }else{
                super.onLoadResource(view, url);
            }
        }
    });
}

Test.Html文件:

<html lang="en">
    <head>
        <meta charset=utf-8>
        <title>Sample HTML5 Structure</title>
        <SCRIPT LANGUAGE="JavaScript">
            function playvideo(url){
            window.location=url;
            }
        </SCRIPT>
    </head>
    <body>
        <video src="file:///sdcard/kummar.m4v" width="300" onclick="playvideo('file:///sdcard/ku<p>testing of video element.</p></video>
    </body>
</html>

我不知道HTML是对还是错(由于缺乏我的HTML知识)。

这是我的Logcat错误:

03-20 20:35:51.911: E/AndroidRuntime(9222): FATAL EXCEPTION: main
03-20 20:35:51.911: E/AndroidRuntime(9222): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.videoManager/com.videoManager.VideoManagerActivity}: java.lang.NullPointerException
03-20 20:35:51.911: E/AndroidRuntime(9222):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-20 20:35:51.911: E/AndroidRuntime(9222):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-20 20:35:51.911: E/AndroidRuntime(9222):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-20 20:35:51.911: E/AndroidRuntime(9222):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-20 20:35:51.911: E/AndroidRuntime(9222):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 20:35:51.911: E/AndroidRuntime(9222):     at android.os.Looper.loop(Looper.java:123)
03-20 20:35:51.911: E/AndroidRuntime(9222):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-20 20:35:51.911: E/AndroidRuntime(9222):     at java.lang.reflect.Method.invokeNative(Native Method)
03-20 20:35:51.911: E/AndroidRuntime(9222):     at java.lang.reflect.Method.invoke(Method.java:521)
03-20 20:35:51.911: E/AndroidRuntime(9222):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-20 20:35:51.911: E/AndroidRuntime(9222):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-20 20:35:51.911: E/AndroidRuntime(9222):     at dalvik.system.NativeStart.main(Native Method)
03-20 20:35:51.911: E/AndroidRuntime(9222): Caused by: java.lang.NullPointerException
03-20 20:35:51.911: E/AndroidRuntime(9222):     at com.videoManager.VideoManagerActivity.onCreate(VideoManagerActivity.java:44)
03-20 20:35:51.911: E/AndroidRuntime(9222):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-20 20:35:51.911: E/AndroidRuntime(9222):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-20 20:35:51.911: E/AndroidRuntime(9222):     ... 11 more

1 个答案:

答案 0 :(得分:1)

如果它崩溃了

webView.getSettings().setPluginState(WebSettings.PluginState.ON);

然后,您的webView对象可能为null或getSettings()返回null。

检查是否

WebView webView = (WebView) findViewById(R.id.webView1);

确实找到了webView,如果findViewById返回“null”,则可能是R.id.webView1所在的xml布局未设置为当前Activity的内容。