在我的应用程序中,我使用这种方法来访问音频文件:
public Cursor pobierzPlikiAudio(){
String[] STAR = {"*"};
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
Uri uri= Uri.parse("content://pl/aplikacja/app/provider/MediaStore/Audio");
return getContentResolver().query(uri, STAR, selection, null, null);
}
但在我的 LogCat 中,我得到一个 nullPointerException。怎么了?
出现错误的 Logcat:
10-29 07:04:17.729: E/AndroidRuntime(24354): FATAL EXCEPTION: main
10-29 07:04:17.729: E/AndroidRuntime(24354): java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.aplikacja.app/pl.aplikacja.app.MainActivity}: java.lang.NullPointerException
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.os.Looper.loop(Looper.java:137)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.main(ActivityThread.java:4441)
10-29 07:04:17.729: E/AndroidRuntime(24354): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 07:04:17.729: E/AndroidRuntime(24354): at java.lang.reflect.Method.invoke(Method.java:511)
10-29 07:04:17.729: E/AndroidRuntime(24354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-29 07:04:17.729: E/AndroidRuntime(24354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-29 07:04:17.729: E/AndroidRuntime(24354): at dalvik.system.NativeStart.main(Native Method)
10-29 07:04:17.729: E/AndroidRuntime(24354): Caused by: java.lang.NullPointerException
10-29 07:04:17.729: E/AndroidRuntime(24354): at pl.aplikacja.app.MainActivity.onCreate(MainActivity.java:18)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.Activity.performCreate(Activity.java:4465)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
10-29 07:04:17.729: E/AndroidRuntime(24354): ... 11 more
这是 MainActivity.java
package pl.aplikacja.app;
import android.os.Bundle;
import android.database.Cursor;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.provider.MediaStore;
public class MainActivity extends Activity {
private Dostawca dostawca;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor tablicaPlikow = dostawca.pobierzPlikiAudio();
if (tablicaPlikow.moveToFirst()) {
String title;
String artist;
String album;
int titleColumn = tablicaPlikow.getColumnIndex(MediaStore.EXTRA_MEDIA_TITLE);
int artistColumn = tablicaPlikow.getColumnIndex(MediaStore.EXTRA_MEDIA_ARTIST);
int albumColumn =tablicaPlikow.getColumnIndex(MediaStore.EXTRA_MEDIA_ALBUM);
// Get the field values
title = tablicaPlikow.getString(titleColumn);
artist = tablicaPlikow.getString(artistColumn);
album = tablicaPlikow.getString(albumColumn);
TextView widok = (TextView) findViewById(R.id.tekst);
widok.setText(""+title+" "+ artist + " " + album);
}
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}