当我运行我的代码时,我得到一个异常@ 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
答案 0 :(得分:0)
这可能没什么用,但我注意到你的投影是一个长度为5的数组,你的字符串构建方法需要第六列(cursor.getString(5))。当然,这不会在managedQuery上导致NullPointerException,你确定发生异常的地方吗?因为这一点看起来没问题(除了您定义选择标准但不使用它的事实)。