MediaStore内容提供商错误(Android)

时间:2011-12-31 06:15:23

标签: android audio nullpointerexception media android-contentprovider

我正在尝试使用MediaStore内容提供商来获取存储在SD卡上的所有音频文件。然后我拿出标题,艺术家,专辑,并将它们添加到各自的阵列中。出于某种原因,每当我运行我的代码时,我得到一个nullPointerException(我将发布LogCat)。任何帮助都会很棒。

以下是代码:

public class LoadAndSplash extends Activity {    

private ArrayList<String> songs = new ArrayList<String>(); 
private ArrayList<String> albums = new ArrayList<String>(); 
private ArrayList<String> artists = new ArrayList<String>();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);   

    Typeface tf = Typeface.createFromAsset(getAssets(),
            "fonts/SALARYMA.ttf");  
    TextView tv = (TextView) findViewById(R.id.splashtitle); 
    tv.setTypeface(tf);  

    Context context = getApplicationContext(); 

    CharSequence text = "Reading Music...";
    int duration = Toast.LENGTH_LONG;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show(); 

    String[] projection = new String[] { 
            MediaStore.EXTRA_MEDIA_TITLE,
            MediaStore.EXTRA_MEDIA_ARTIST, 
            MediaStore.EXTRA_MEDIA_ALBUM 

    };

    // Get the base URI for the People table in the Contacts content provider.
    Uri media =  MediaStore.getMediaScannerUri();

            // Make the query. 
    Cursor mediaCursor = managedQuery(media,
            projection, // Which columns to return 
            null,       // Which rows to return (all rows)
            null,       // Selection arguments (none)
            // Put the results in ascending order by name
            MediaStore.EXTRA_MEDIA_TITLE + " ASC"); 

    if (mediaCursor.moveToFirst()) {

        String title; 
        String artist;  
        String album; 
        int titleColumn = mediaCursor.getColumnIndex(MediaStore.EXTRA_MEDIA_TITLE); 
        int artistColumn = mediaCursor.getColumnIndex(MediaStore.EXTRA_MEDIA_ARTIST);
        int albumColumn = mediaCursor.getColumnIndex(MediaStore.EXTRA_MEDIA_ALBUM); 


        do {
            // Get the field values
            title = mediaCursor.getString(titleColumn);
            artist = mediaCursor.getString(artistColumn); 
            album = mediaCursor.getString(albumColumn);

            artists.add(artist); 
            albums.add(album); 
            songs.add(title);


        } while (mediaCursor.moveToNext());


    //new MusicGetter().execute();

} 
} 
} 

错误日志的前几行:

12-31 00:59:50.454: E/AndroidRuntime(4041): FATAL EXCEPTION: main
12-31 00:59:50.454: E/AndroidRuntime(4041): java.lang.RuntimeException: Unable to
start          activity
ComponentInfo{com.adam.mediaplayer/com.adam.mediaplayer.LoadAndSplash}:
java.lang.NullPointerException

12-31 00:59:50.454: E/AndroidRuntime(4041):     at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
 12-31 00:59:50.454: E/AndroidRuntime(4041):    at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
12-31 00:59:50.454: E/AndroidRuntime(4041):     at
android.app.ActivityThread.access$1500(ActivityThread.java:132)
12-31 00:59:50.454: E/AndroidRuntime(4041):     at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)

0 个答案:

没有答案