调用在Android中具有getContentResolver()。查询的方法时的NPE

时间:2014-09-30 02:44:44

标签: java android android-cursor mediastore

从Android中的其他活动调用 getPlaylist()方法时出现NullPointerException。 该方法应该获取音频信息,然后使用 Cursor cursor = getContentResolver()。query(MediaStore ...

返回它

以下是详细信息。

我正在使用两项活动(AudioPlayerActivity,FileManager)处理自定义音频播放器。 AudioPlayerActivity使用FileManager **获取音频文件列表,然后播放它们。

以下是他们的片段。以下是 AudioPlayerActivity.java

ArrayList<HashMap<String, String>> audioList = new ArrayList<HashMap<String, String>>();

mp = new MediaPlayer();
fileManager = new FileManager();

audioList = fileManager.getPlaylist(); // NPE when calling this method!

playAudio(0);

接下来是 FileManager.java ,它使用 MediaStore API读取所有音频文件并在ArrayList中存储详细信息(如MP3 ID标签)。

public class FileManager extends Activity {

    @Override
    public void onCreate(Bundle SavedInstanceState) {
        super.onCreate(savedInstanceState);
        // no view. just getting audio files
    }

    public ArrayList<HashMap<String, String> getPlaylist() {
        String[] proj = { MediaStore.Audio.Media._ID };

        Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null}

        Log.d("debug", "is cursor?"); // NPE here!

        return list;
    }

logcat的

09-30 11:55:35.265: D/AndroidRuntime(13516): Shutting down VM
09-30 11:55:35.265: W/dalvikvm(13516): threadid=1: thread exiting with uncaught exception (group=0x41d37c08)
09-30 11:55:35.270: E/AndroidRuntime(13516): FATAL EXCEPTION: main
09-30 11:55:35.270: E/AndroidRuntime(13516): Process: project.sample.audioplayer, PID: 13516
09-30 11:55:35.270: E/AndroidRuntime(13516): java.lang.RuntimeException: Unable to start activity ComponentInfo{project.sample.audioplayer/project.sample.audioplayer.AudioPlayerActivity}: java.lang.NullPointerException
09-30 11:55:35.270: E/AndroidRuntime(13516):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at android.os.Looper.loop(Looper.java:157)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at android.app.ActivityThread.main(ActivityThread.java:5356)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at java.lang.reflect.Method.invokeNative(Native Method)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at java.lang.reflect.Method.invoke(Method.java:515)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at dalvik.system.NativeStart.main(Native Method)
09-30 11:55:35.270: E/AndroidRuntime(13516): Caused by: java.lang.NullPointerException
09-30 11:55:35.270: E/AndroidRuntime(13516):    at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:104)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at project.sample.audioplayer.FileManager.getPlaylist(FileManager.java:35)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at project.sample.audioplayer.AudioPlayerActivity.onCreate(AudioPlayerActivity.java:70)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at android.app.Activity.performCreate(Activity.java:5426)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
09-30 11:55:35.270: E/AndroidRuntime(13516):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
09-30 11:55:35.270: E/AndroidRuntime(13516):    ... 11 more

它在 getContentResolver()。query()的行中提供 NullPointerException

我错过了什么?或者是不可能在方法而不是 onCreate()方法中使用Cursor cursor = getContentResolver()。query(MediaStore ...)?

我非常确定AudioPlayerAcitivity.java不是问题,因为它在FileManger.java中使用文件类而不是MediaStore时可以正常工作。

1 个答案:

答案 0 :(得分:1)

需要使用适当的上下文调用

getContentResolver()

不是从Activity扩展FileManager,只需从调用活动(AudioPlayerActivity.java)传入上下​​文,然后就可以使用Cursor cursor = context.getContentResolver()...中的上下文