1

我正在尝试在以下代码中运行一个线程,但它由于某种未知原因而崩溃。这是代码->

public class StaticRef extends Activity implements Runnable
{   
Cursor cursor;
Thread tSearchSongs=new Thread(this,"SearchSongs");
WritingXML wxml;

public void searchforsongs()
{
    tSearchSongs.start();
}

@Override
public void run() 
{
    Looper.prepare();
    try
    {
    wxml=new WritingXML();
    String[] STAR = { "*" };        
    Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    cursor = managedQuery(allsongsuri, STAR, selection, null, null);
    if (cursor != null) 
    {
        if (cursor.moveToFirst())
        {
            do 
            {
                //SongName
                String song_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                //SongID
                int song_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
                //SongPath
                String fullpath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                //Song's album name
                String album_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                //Song's album ID
                int album_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                //Song's artist name
                String artist_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                //Song's artist ID
                int artist_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
                try
                {
                    wxml.AddDatatoXML(Integer.toString(song_id) , song_name, fullpath);
                }
                catch(Exception exp)
                {
                    Log.e("~~Exception Occurred in StaticRef~~", exp.toString());
                }
            } while (cursor.moveToNext());
        }
        cursor.close();
    }
    }
    catch(NullPointerException exp)
    {
        Log.e("~~Null Pointer Exception~~",exp.toString());
        android.util.Log.e("->>" , "~~stacktrace~~", exp);
    }
    catch(Exception exp)
    {
        Log.e("~~Exception~~",exp.toString());
    }
}

}

以下是它的LOGCAT

08-29 13:54:03.149: E/~~Null Pointer Exception~~(13899): java.lang.NullPointerException
08-29 13:54:03.159: E/->>(13899): ~~stacktrace~~
08-29 13:54:03.159: E/->>(13899): java.lang.NullPointerException
08-29 13:54:03.159: E/->>(13899):   at   android.content.ContextWrapper.getContentResolver(ContextWrapper.java:91)
08-29 13:54:03.159: E/->>(13899):   at android.app.Activity.managedQuery(Activity.java:1708)
08-29 13:54:03.159: E/->>(13899):   at com.example.boombastic.StaticRef.run(StaticRef.java:53)
08-29 13:54:03.159: E/->>(13899):   at java.lang.Thread.run(Thread.java:856)

我还想知道什么是 looper 以及它在线程执行中的重要性?

4

2 回答 2

2

如果您managedQuery()在活动的 onCreate() 之前调用该方法,则该方法将没有有效的上下文。有关更多信息,请参阅此问题:为什么 Application 中的 getContentResolver() 会导致 NullPointerException?

于 2013-08-29T08:47:18.203 回答
0

Looper 是用于运行消息循环的线程。默认情况下,线程没有消息,因此您必须自己实现它们。

至于 NPE,请查看 allprog 的答案。

于 2013-08-29T08:54:41.223 回答