0

我知道这篇文章就像这个问题: 如何获取已安装媒体播放器的列表, 但我试图测试代码并且它崩溃了!这是代码:

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

有什么想法吗?

4

1 回答 1

1

可能没有任何内容Uri,因为你正在编造它。请使用Uri实际内容。

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


更新

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

既然packageManagernull,你会在NullPointerException这里得到一个。用于getPackageManager()获取PackageManager.

于 2012-10-19T12:28:44.173 回答