1

I am trying to run a thread in the following code but it is crashing by some unknown reason. This is the code ->

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());
    }
}

}

The following is its 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)

I would also like to know what is looper and its significant importance in a thread execution?

2 Answers2

2

If you call the managedQuery() method before the activity's onCreate(), then the method will not have a valid context. See this question for more info: why does getContentResolver() within Application cause NullPointerException?

Community
  • 1
  • 1
allprog
  • 16,540
  • 9
  • 56
  • 97
  • I guess I have made a mistake. StaticRef does not have an on create method, well I extended Activity, because then managedQuery function would have not been accessible. – user2714061 Aug 29 '13 at 08:54
  • It will be ok, but make sure that you start the thread only from the onCreate method or later – allprog Aug 29 '13 at 08:59
  • IMO you should probably revise your architecture. You can create an `AsyncTask` in your real activity, and that way you'll have access to the `manageQuery()` method. – Itai Hanski Aug 29 '13 at 09:01
  • @ItaiHanski I am new to android, sorry to ask but could you explain what changes should I make in the above code to achieve this then? – user2714061 Aug 29 '13 at 09:04
  • In your real `Activity` you can create an inner / anonymous class that extends `AsyncTask` that way it has a reference to its super class - the activity, and to its methods. Try reading about here: http://developer.android.com/reference/android/os/AsyncTask.html – Itai Hanski Aug 29 '13 at 09:06
0

The Looper is the thread used to run a message loop. Threads don't have messages by default so you have to implement them yourself.

As for the NPE, check out allprog's answer.

Injustice
  • 38
  • 6