android-list已安装的媒体播放器

时间:2012-10-19 10:34:07

标签: android android-mediaplayer

我知道这篇文章就像这个问题: How to get a list of installed media players 但我试图测试代码,它崩溃了! 这是代码:

public class Newactivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.display);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(MediaStore.Video.Media.INTERNAL_CONTENT_URI,"1"); 
intent.setData(uri);
List<ResolveInfo> playerList;
PackageManager packageManager = null;
playerList = packageManager.queryIntentActivities(intent, 0);
    }

这是logcat错误:

10-19 13:05:52.770: E/AndroidRuntime(558): FATAL EXCEPTION: main
10-19 13:05:52.770: E/AndroidRuntime(558): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.upnpexample/com.upnpexample.Newactivity}: java.lang.NullPointerException
10-19 13:05:52.770: E/AndroidRuntime(558):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-19 13:05:52.770: E/AndroidRuntime(558):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-19 13:05:52.770: E/AndroidRuntime(558):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-19 13:05:52.770: E/AndroidRuntime(558):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-19 13:05:52.770: E/AndroidRuntime(558):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 13:05:52.770: E/AndroidRuntime(558):  at android.os.Looper.loop(Looper.java:123)
10-19 13:05:52.770: E/AndroidRuntime(558):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-19 13:05:52.770: E/AndroidRuntime(558):  at java.lang.reflect.Method.invokeNative(Native Method)
10-19 13:05:52.770: E/AndroidRuntime(558):  at java.lang.reflect.Method.invoke(Method.java:507)
10-19 13:05:52.770: E/AndroidRuntime(558):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-19 13:05:52.770: E/AndroidRuntime(558):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-19 13:05:52.770: E/AndroidRuntime(558):  at dalvik.system.NativeStart.main(Native Method)
10-19 13:05:52.770: E/AndroidRuntime(558): Caused by: java.lang.NullPointerException
10-19 13:05:52.770: E/AndroidRuntime(558):  at com.upnpexample.Newactivity.onCreate(Newactivity.java:31)
10-19 13:05:52.770: E/AndroidRuntime(558):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-19 13:05:52.770: E/AndroidRuntime(558):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-19 13:05:52.770: E/AndroidRuntime(558):  ... 11 more

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

Uri可能没有任何内容,因为您正在制作它。请使用Uri表示实际内容。

除此之外,检查LogCat中的堆栈跟踪,看看你可能做错了什么。


<强>更新

PackageManager packageManager = null;
playerList = packageManager.queryIntentActivities(intent, 0);

由于packageManagernull,您将在此处获得NullPointerException。使用getPackageManager()获取PackageManager的实例。