gpt4 book ai didi

java - 空指针@ ManagedQuery

转载 作者:行者123 更新时间:2023-12-01 05:39:15 25 4
gpt4 key购买 nike

当我运行我的代码时,我收到一个异常@ 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;
}

}

这是异常(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 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com