Android视频嵌入 [英] Android video embedding

查看:154
本文介绍了Android视频嵌入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有取从网页一些文本,并将其显示在屏幕上的应用程序。我想文后,把视频上传到网络。我怎么能这样做呢?如果我喜欢下面的code,应用程序崩溃(空指针异常)。

I have an application that fetches some text from a web page , and displays it on the screen. I would like to put after the text a video from the web . How could I do that ? If I do like the following code , the application crashes ( Null pointer exception) .

我的code:

LinearLayout tt= (LinearLayout)findViewById(R.id.kiu);
WebView webview;
tt.addView(fin); // fin is the TextView


        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("http://www.youtube.com/embed/j4Wmjl7jxQo");
        tt.addView(webview);

main.xml中

main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:scrollbars="vertical"

    android:background="#FFFFFF"

    >
<LinearLayout
android:id="@+id/kiu"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="0dp">

  </LinearLayout>
</ScrollView>

我想不出我该怎么办..我想我的应用程序,以显示 这个 而不是 这个

推荐答案

您应该从异常张贴堆栈跟踪。并突出不知它指向哪条线路。但是从我这里头的顶部是我注意到:

You should post the stack trace from your exception. And highlight somehow which line it is pointing to. But off the top of my head here is what I notice:

    webview = (WebView) findViewById(R.id.webview);
    ...
    tt.addView(webview);

除非该观点已经在屏幕上findViewById(ID)将返回null。你似乎并没有把在XML布局中的WebView(我认为是一个目前这个时候code正在执行在屏幕上)。既然你还没有一个在那里findViewById将返回null你和下一行,当您尝试使用一个方法,你会得到一个空指针。

findViewById(id) will return null unless the view is already on the screen. You don't seem to have a WebView in your xml layout (which I assume is the one that is currently on the screen when this code is executing). Since you don't have one in there findViewById will return null to you and on the next line when you try to use a method you'll get a null pointer.

您有几种选择基本上是什么它归结为是,虽然你不会需要同时findViewById和.addView(),你只需要一个或其他。

You have a few options basically what it comes down to though is you won't need both findViewById and .addView(), you'll only need one or the other.

您可以:

替换的WebView =(的WebView)findViewById(R.id.webview); 用的WebView constroctor像这样:的WebView =新的WebView(这);

replace webview = (WebView) findViewById(R.id.webview); with a WebView constroctor like so: webview = new WebView(this);

或者

您可以将的WebView添加到您的XML,离开findViewById它是如何,但现在拿出 tt.addView(的WebView);

you can add a WebView to your xml, leave findViewById how it is now but take out tt.addView(webview);

另外要注意,因为你在网页视图显示页面的YouTube用户会看到整个页面,而不仅仅是一个全屏幕的电影。而且它不会立即启动,他们将不得不preSS启动按钮开始下载。如果你正在寻找的东西,只是自动播放视频并没有显示任何你会想要一个的 VideoView

Also be aware that since you're displaying a youtube page in a webview the user is going to see the whole page, not just a full screen movie. And it is not going to start immediately, they will have to press the start button to get it going. If you are looking for something to just play the video automatically and not show anything else you'll want a VideoView

<一个href=\"http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/VideoViewDemo.html\"相对=nofollow>这里是行动VideoView一个相当简单的例子你只需将您的网址粘贴到PATH变量。虽然我不能确定,如果它知道如何去code YouTube网址或没有。

Here is a fairly straight forward example of VideoView in action You simply have to paste your url into the path variable. Although I am unsure if it knows how to decode a youtube url or not.

这篇关于Android视频嵌入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆