English 中文(简体)
Null Pointer 试图与媒体保持声望的例外情况
原标题:NullPointerException when trying to play a sound with MediaPlayer

因此,我有一个简单的机体类型申请,通过一份名单来操作。 标注一个物品,它具有声望。 我有一个公开的静态内涵,名称是健全的ResourceList,其条目包括R.raw.foo, R.raw。 在《清单》中与案文有关的条目的顺序相同。

该守则为包括我本人在内的许多用户服务,但最近张贴了Null PointerException,特别是从“CompletionListener”线上张贴。 对我来说,这意味着媒体主持人。 造成返回无效,但我无法说明为什么会失败,而并非每个人都会失败。

public void onListItemClick(ListView parent, View v, int position, long id) {
    MediaPlayer mp = MediaPlayer.create(getBaseContext(), soundResourceList[position]);
    mp.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();
        }
    });
    mp.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });
}

Normally a NPE with MediaPlayer is due to something like not releasing its previous instance, but I already have that issue handled as shown in the code. So what am I missing that might cause the MediaPlayer.create() to fail?

问题回答

http://developer.android.com/vis/android/media/MediaPlayer.html#create”rel=“nofollow”>create 方法:

Returns
    a MediaPlayer object, or null if creation failed

由于可能为每个媒体Player分配一些低水平资源,如果总经理忙忙忙忙忙忙忙忙忙忙忙忙,则媒体分配会失败。

如果这些声音都是相对活跃的传闻,那么你可以查看android.media.SoundPool/a>。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签