2

我正在制作一个 webview 应用程序,它显示其中包含 html5 视频的页面。当我点击全屏时,视频消失。

当我搜索相同的问题时,看到了这个问题,这对我来说是同样的问题,并且认为第二个答案(这里)也适合我。即使我使用答案中给出的类,我仍然遇到这些错误并且应用程序停止了。

05-03 13:41:50.064: E/AndroidRuntime(19292): FATAL EXCEPTION: main
05-03 13:41:50.064: E/AndroidRuntime(19292): java.lang.NullPointerException
05-03 13:41:50.064: E/AndroidRuntime(19292):    at com.zapkolik.mayis.VideoEnabledWebChromeClient.onShowCustomView(VideoEnabledWebChromeClient.java:132)
05-03 13:41:50.064: E/AndroidRuntime(19292):    at android.webkit.HTML5VideoFullscreen.enterFullscreen(HTML5VideoFullscreen.java:239)
05-03 13:41:50.064: E/AndroidRuntime(19292):    at android.webkit.HTML5VideoView.enterFullscreenVideoState(HTML5VideoView.java:544)
05-03 13:41:50.064: E/AndroidRuntime(19292):    at android.webkit.HTML5VideoViewProxy$VideoPlayer.enterFullscreenVideo(HTML5VideoViewProxy.java:182)
05-03 13:41:50.064: E/AndroidRuntime(19292):    at android.webkit.HTML5VideoViewProxy.handleMessage(HTML5VideoViewProxy.java:479)
05-03 13:41:50.064: E/AndroidRuntime(19292):    at android.os.Handler.dispatchMessage(Handler.java:99)
.
.
.

我的活动在这里:

MainActivity.java

package com.zapkolik.mayis;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.text.method.HideReturnsTransformationMethod;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

private VideoEnabledWebView webView;
private VideoEnabledWebChromeClient webChromeClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Save the web view
    webView = (VideoEnabledWebView) findViewById(R.id.webView);

    // Initialize the VideoEnabledWebChromeClient and set event handlers
    View nonVideoLayout = findViewById(R.id.nonVideoLayout); // Your own view, read class comments
    ViewGroup videoLayout = (ViewGroup) findViewById(R.layout.video_layout); // Your own view, read class comments
    View loadingView = getLayoutInflater().inflate(R.layout.loading_video, null);
// Your own view, read class comments

    final Activity activity = this;
    webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView) // See all available constructors...
    {
        // Subscribe to standard events, such as onProgressChanged()...

        @Override
        public void onProgressChanged(WebView view, int progress)
        {
            activity.setProgress(progress * 1000);
        }
    };
    webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
    {
        @Override
        public void toggledFullscreen(boolean fullscreen)
        {
//              AlertDialog alertDialog = new AlertDialog.Builder(activity).create(); //Read Update
//              alertDialog.setTitle("hi");
//              alertDialog.setMessage("this is my app");
        }
    });
    webView.setWebChromeClient(webChromeClient);
    webView.setWebViewClient(new WebViewClient());

    // Navigate everywhere you want, this classes have only been tested on YouTube's mobile site
    webView.loadUrl("http://m.youtube.com");
}

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

@Override
public void onBackPressed()
{
    // Notify the VideoEnabledWebChromeClient, and handle it ourselves if it doesn't handle it
    if (!webChromeClient.onBackPressed())
    {
        if (webView.canGoBack())
        {
            webView.goBack();
        }
        else
        {
            // Close app (presumably)
            super.onBackPressed();
        }
    }
}

}

和布局是:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nonVideoLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.zapkolik.mayis.VideoEnabledWebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

加载视频.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/center"
    android:gravity="center|center_vertical"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="208dp"
        android:layout_height="0dip"
        android:layout_weight="0.39" />

</LinearLayout>

video_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<VideoView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/videoView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

当我点击全屏按钮时,我无法弄清楚为什么会出现错误。

4

0 回答 0