运行代码时,出现异常@managedQuery。
怎么了?
我没看到
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;
}
}
这是例外
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 ComponentInfo{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
最佳答案
这可能没有帮助,但是我注意到您的投影是一个长度为5的数组,并且您的字符串构建方法需要第六列(cursor.getString(5))。当然,这不会在managedQuery上引起NullPointerException,您确定那是发生异常的地方吗?因为从这里开始看起来不错(除了您定义选择标准但不使用它的事实)。
关于java - 空指针@managedQuery,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7569874/