开发者

Nullpointer @ managedQuery

When I run my code, I get an exception @ managedQuery. Whats wrong? I don't see it.

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;

public class FileManager extends Activity
{

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    }

public List<String> getAudioFiles()
{
    List<String> songs = new ArrayList<String>();

    //Some audio may be explicitly marked as not being music
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

    String[] projection = {
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DURATION
    };

    Cursor cursor = managedQuery(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            projection,
            null,
            null,
            MediaStore.Audio.Media._ID);

    while(cursor.moveToNext()){
            songs.add(cursor.getString(0) + "||" + cursor.getString(1) + "||" +   cursor.getString(2) + "||" +   cursor.getString(3) + "||" +  cursor.getString(4) + "||" +  cursor.getString(5));
    }

    return songs;
}

}

This is the exception

   09-27 17:04:20.130: ERROR/AndroidRuntime(4079): FATAL EXCEPTION: main
09-27 17:04:20.130: ERROR/AndroidRuntime(4079): java.lang.RuntimeException: Unable to start activity Compone开发者_如何学PythonntInfo{com.test/com.test.testActivity}: java.lang.NullPointerException
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at android.os.Looper.loop(Looper.java:130)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at android.app.ActivityThread.main(ActivityThread.java:3691)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at java.lang.reflect.Method.invokeNative(Native Method)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at java.lang.reflect.Method.invoke(Method.java:507)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at dalvik.system.NativeStart.main(Native Method)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079): Caused by: java.lang.NullPointerException
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:90)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at android.app.Activity.managedQuery(Activity.java:1556)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at com.test.FileManager.getAudioFiles(FileManager.java:35)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at com.test.testActivity.onCreate(testActivity.java:21)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
09-27 17:04:20.130: ERROR/AndroidRuntime(4079):     ... 11 more


This may not be helpful but I note that your projection is an array with a length of five and your string building method expects a sixth column (cursor.getString(5)). Of course, that wouldn't cause a NullPointerException at managedQuery, are you sure that's where the exception is occuring? Because that bit looks okay from here (apart from the fact that you define selection criteria but don't use it).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜