Android 视频嵌入
我有一个应用程序,可以从网页中获取一些文本,并将其显示在屏幕上。我想在文字后面放一段来自网络的视频。我怎么能这么做呢?如果我确实喜欢下面的代码,应用程序就会崩溃(空指针异常)。
我的代码:
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
<?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>
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) .
My 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
<?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>
I can't figure out what should I do .. I would like my app to display this instead of this .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该发布异常的堆栈跟踪。并以某种方式突出显示它指向哪一行。但我注意到的是:
findViewById(id) 将返回 null,除非视图已经在屏幕上。您的 xml 布局中似乎没有 WebView(我假设是执行此代码时屏幕上当前显示的 WebView)。由于您没有其中的 findViewById 将返回 null 给您,并且在下一行当您尝试使用方法时您将得到一个空指针。
您有几个选择,基本上可以归结为您不需要同时使用 findViewById 和 .addView(),您只需要其中之一。
您可以:
将
webview = (WebView) findViewById(R.id.webview);
替换为 WebView 构造函数,如下所示:webview = new WebView(this);
或者
您可以将 WebView 添加到您的 xml 中,保留 findViewById 现在的样子,但取出 tt.addView(webview);
另请注意,由于您在 webview 中显示 YouTube 页面,因此用户将看到整个页面,而不仅仅是全屏电影。而且它不会立即启动,他们必须按下启动按钮才能启动。如果您正在寻找仅自动播放视频而不显示任何其他内容的东西,您将需要 VideoView
这里是一个相当简单的 VideoView 实际示例 您只需将 url 粘贴到路径变量中即可。虽然我不确定它是否知道如何解码 YouTube 网址。
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:
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.
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.
You can either:
replace
webview = (WebView) findViewById(R.id.webview);
with a WebView constroctor like so:webview = new WebView(this);
Or
you can add a WebView to your xml, leave findViewById how it is now but take out
tt.addView(webview);
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
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.