内容解析程序查询NullException

时间:2013-07-19 21:09:49

标签: android nullpointerexception android-contentresolver

我正在尝试使用存储在外部存储卷上的音乐填充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让我无处可去。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

添加

cr = getContentResolver();

这将使ContentResolver返回当前上下文。