0

我正在尝试用存储在外部存储卷上的音乐填充 ListView。

我已经将代码从一个更大的项目中分离出来,但错误是一样的。在我隔离的测试程序中,我有一个按钮,按下后应该填充一个 ListView。我已经标记了引发错误的行。

package com.example.crtest;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    ListView mList;
    Cursor cu;
    ContentResolver cr;

    public void fillMeUp(View view) {
        Log.i("FillMeUp", "FMU start.");
        String[] proj = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.ALBUM };
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Log.i("FillMeUp", uri.toString());

        // This is where I get an FATAL EXCEPTION
        cu = cr.query(uri, proj, MediaStore.Audio.Media.IS_MUSIC + "=1", null, null);
        Log.i("FillMeUp", "Query finished. " + (cu == null ? "Returned NULL." : "Returned a cursor."));

        Log.i("FillMeUp", "FMU with some text.");

        mList = (ListView) findViewById(R.id.lv);
        @SuppressWarnings("deprecation")
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                this,
                android.R.layout.simple_list_item_1,
                cu,
                new String[] { "bla" },
                new int[] { android.R.id.text1, android.R.id.text2 }
                );
        mList.setAdapter(adapter);
        Log.i("FillMeUp", "FMU mlist done.");
    }
}

这就是 LogCat 吐出来的。

07-19 23:07:09.613: E/Trace(30502): error opening trace file: No such file or directory (2)
07-19 23:07:09.763: D/libEGL(30502): loaded /system/lib/egl/libEGL_mali.so
07-19 23:07:09.763: D/libEGL(30502): loaded /system/lib/egl/libGLESv1_CM_mali.so
07-19 23:07:09.768: D/libEGL(30502): loaded /system/lib/egl/libGLESv2_mali.so
07-19 23:07:09.768: E/(30502): Device driver API match
07-19 23:07:09.768: E/(30502): Device driver API version: 17
07-19 23:07:09.768: E/(30502): User space API version: 17
07-19 23:07:09.773: E/(30502): mali: REVISION=Linux-r3p1-01rel1 BUILD_DATE=Mon May 13 15:55:05 KST 2013
07-19 23:07:09.803: D/OpenGLRenderer(30502): Enabling debug mode 0
07-19 23:07:12.408: I/FillMeUp(30502): FMU start.
07-19 23:07:12.408: I/FillMeUp(30502): content://media/external/audio/media
07-19 23:07:12.408: D/AndroidRuntime(30502): Shutting down VM
07-19 23:07:12.408: W/dalvikvm(30502): threadid=1: thread exiting with uncaught exception (group=0x40bcc930)
07-19 23:07:12.423: E/AndroidRuntime(30502): FATAL EXCEPTION: main
07-19 23:07:12.423: E/AndroidRuntime(30502): java.lang.IllegalStateException: Could not execute method of the activity
07-19 23:07:12.423: E/AndroidRuntime(30502):     at android.view.View$1.onClick(View.java:3606)
07-19 23:07:12.423: E/AndroidRuntime(30502):     at android.view.View.performClick(View.java:4211)
07-19 23:07:12.423: E/AndroidRuntime(30502):     at android.view.View$PerformClick.run(View.java:17362)
07-19 23:07:12.423: E/AndroidRuntime(30502):     at android.os.Handler.handleCallback(Handler.java:725)
07-19 23:07:12.423: E/AndroidRuntime(30502):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-19 23:07:12.423: E/AndroidRuntime(30502):     at android.os.Looper.loop(Looper.java:137)
07-19 23:07:12.423: E/AndroidRuntime(30502):     at android.app.ActivityThread.main(ActivityThread.java:5227)
07-19 23:07:12.423: E/AndroidRuntime(30502):     at java.lang.reflect.Method.invokeNative(Native Method)
07-19 23:07:12.423: E/AndroidRuntime(30502):     at java.lang.reflect.Method.invoke(Method.java:511)
07-19 23:07:12.423: E/AndroidRuntime(30502):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-19 23:07:12.423: E/AndroidRuntime(30502):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
07-19 23:07:12.423: E/AndroidRuntime(30502):     at dalvik.system.NativeStart.main(Native Method)
07-19 23:07:12.423: E/AndroidRuntime(30502): Caused by: java.lang.reflect.InvocationTargetException
07-19 23:07:12.423: E/AndroidRuntime(30502):     at java.lang.reflect.Method.invokeNative(Native Method)
07-19 23:07:12.423: E/AndroidRuntime(30502):     at java.lang.reflect.Method.invoke(Method.java:511)
07-19 23:07:12.423: E/AndroidRuntime(30502):     at android.view.View$1.onClick(View.java:3601)
07-19 23:07:12.423: E/AndroidRuntime(30502):     ... 11 more
07-19 23:07:12.423: E/AndroidRuntime(30502): Caused by: java.lang.NullPointerException
07-19 23:07:12.423: E/AndroidRuntime(30502):     at com.example.crtest.MainActivity.fillMeUp(MainActivity.java:44)
07-19 23:07:12.423: E/AndroidRuntime(30502):     ... 14 more

我对 Android 比较陌生,但我以前使用过 Java。我的编程技巧虽然生疏,但通常还可以。但我在这里不知所措。两天来一直在解决这个问题,使用 Google 和 Stack Overflow 让我无处可去。

4

2 回答 2

0

您的内容解析器为空,如果您想获取保存在外部存储上的所有音乐,请使用 context.managedQuery。看这个:Android external storageGet filename and path from URI from mediastore

于 2013-07-19T23:07:13.970 回答
0

添加

cr = getContentResolver();

这将返回与当前上下文相对应的 ContentResolver。

于 2014-06-06T13:39:44.780 回答