假设我有一个网址(不具体,它将是一个YouTube链接)
点击缩略图/按钮后,我想点开一个弹出式排序,在android中询问要使用哪个玩家。
我知道有很多文章,但我无法让它发挥作用。
我尝试了这个:
package com.example.videotry;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
public class Play extends Activity implements OnClickListener {
@Override
public void onClick(View v) {
if (v.getId() == R.id.imageButton1) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setType("video/*");
intent.setData(Uri.parse("http://www.youtube.com/embed/PW_88sL_2dA?feature=oembed" ));
startActivity(Intent.createChooser(intent, "Complete action using"));
}
}
}
但它会引发错误:
07-22 15:08:00.680: E/AndroidRuntime(8163): java.lang.NullPointerException
07-22 15:08:00.680: E/AndroidRuntime(8163): at android.app.Activity.startActivityForResult(Activity.java:3190)
07-22 15:08:00.680: E/AndroidRuntime(8163): at android.app.Activity.startActivity(Activity.java:3297)
07-22 15:08:00.680: E/AndroidRuntime(8163): at com.example.videotry.Play.onClick(Play.java:18)
07-22 15:08:00.680: E/AndroidRuntime(8163): at android.view.View.performClick(View.java:3511)
07-22 15:08:00.680: E/AndroidRuntime(8163): at android.view.View$PerformClick.run(View.java:14110)
07-22 15:08:00.680: E/AndroidRuntime(8163): at android.os.Handler.handleCallback(Handler.java:605)
07-22 15:08:00.680: E/AndroidRuntime(8163): at android.os.Handler.dispatchMessage(Handler.java:92)
07-22 15:08:00.680: E/AndroidRuntime(8163): at android.os.Looper.loop(Looper.java:137)
07-22 15:08:00.680: E/AndroidRuntime(8163): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-22 15:08:00.680: E/AndroidRuntime(8163): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 15:08:00.680: E/AndroidRuntime(8163): at java.lang.reflect.Method.invoke(Method.java:511)
07-22 15:08:00.680: E/AndroidRuntime(8163): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-22 15:08:00.680: E/AndroidRuntime(8163): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-22 15:08:00.680: E/AndroidRuntime(8163): at dalvik.system.NativeStart.main(Native Method)
这与视频格式无关。我试过不同的格式。 我正在使用android 4.0
我也尝试了ACTION_VIEW
答案 0 :(得分:1)
你试过
吗? Intent intent = new Intent(Intent.ACTION_VIEW);
而不是
Intent intent = new Intent(Intent.ACTION_MAIN);
我认为它可以帮助您:Play youtube video from URL in android
答案 1 :(得分:0)
替换Intent类的构造函数,
Intent intent = new Intent(Intent.ACTION_VIEW);