我正在試圖用存儲在外部存儲捲上的音樂填充ListView。內容解析器查詢NullException
我已經分離出一個較大的項目的代碼,但錯誤是一樣的。在我隔離的測試程序中,我有一個按下的按鈕,應該填充一個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,現在正在處理這個問題兩天了。 溢出讓我無處可尋。
你在哪裏調用'fillMeUp'方法和其中ContentResolver的初始化? – Desert