Luverious
Luverious

Reputation: 33

Getting Can't create handler inside thread that has not called Looper.prepare() with code that worked for Android 4

I have searched through many similar questions and answers here, but I still can't seem to understand why my code is throwing this error.

12-27 12:57:57.908: W/dalvikvm(465): Exception Ljava/lang/RuntimeException; thrown while initializing Landroid/os/AsyncTask;
12-27 12:57:57.908: W/dalvikvm(465): threadid=9: thread exiting with uncaught exception (group=0x40014760)
12-27 12:57:57.927: E/AndroidRuntime(465): FATAL EXCEPTION: Thread-10
12-27 12:57:57.927: E/AndroidRuntime(465): java.lang.ExceptionInInitializerError
12-27 12:57:57.927: E/AndroidRuntime(465):  at com.verious.baseballnews.NewsParser.fetchNews(NewsParser.java:137)
12-27 12:57:57.927: E/AndroidRuntime(465):  at com.verious.baseballnews.MLBNews.getLeagueNews(MLBNews.java:145)
12-27 12:57:57.927: E/AndroidRuntime(465):  at com.verious.baseballnewsreader.BaseballNewsReader.licenseCheckCompleted(BaseballNewsReader.java:45)
12-27 12:57:57.927: E/AndroidRuntime(465):  at com.verious.lm.LicenseMgr.respondFromCache(LicenseMgr.java:218)
12-27 12:57:57.927: E/AndroidRuntime(465):  at com.verious.lm.LicenseMgr.run(LicenseMgr.java:139)
12-27 12:57:57.927: E/AndroidRuntime(465):  at java.lang.Thread.run(Thread.java:1020)
12-27 12:57:57.927: E/AndroidRuntime(465): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-27 12:57:57.927: E/AndroidRuntime(465):  at android.os.Handler.<init>(Handler.java:121)
12-27 12:57:57.927: E/AndroidRuntime(465):  at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:595)
12-27 12:57:57.927: E/AndroidRuntime(465):  at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:595)
12-27 12:57:57.927: E/AndroidRuntime(465):  at android.os.AsyncTask.<clinit>(AsyncTask.java:184)
12-27 12:57:57.927: E/AndroidRuntime(465):  ... 6 more

There are two puzzles I have here:

  1. My code works with Android 4.0, but got the "Can't create handler inside thread that has not called Looper.prepare" error for all lower versions.
  2. Even when I emptied the doInBackground() call so it only returns a "", I still got the error.

I am trying to get some news items from a web service using AsyncTask and show them on a view. Here is my basic code:

public class News {

    ...
    new FetchXML().execute(url);
    ...

    private class FetchXML extends AsyncTask<String,String,String> {
    protected String doInBackground(final String... urls) {

        String urlToOpen = urls[0];

        URL url;
        String inputLine = null;
        String response = "";
        URLConnection connection;

        try {
            String authStringEnc = b64encode("user:pass".getBytes());
                        url = new URL(urlToOpen);
            connection = url.openConnection();
            connection.setRequestProperty("Authorization", "Basic " + authStringEnc);

            BufferedReader in = new BufferedReader(
                new InputStreamReader(
                connection.getInputStream()));

            while ((inputLine = in.readLine()) != null) { 
                response += inputLine;
            }
            in.close();
        } catch(MalformedURLException e) {
            //
        } catch(IOException e) {
            //
        }

        return response;
    }

    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        NewsParser.this.parse(result);
    }
}

The NewsParser parse() method parses xml and update the view.

I am at a total loss here. Any help will be greatly appreciated!

Thanks!

Upvotes: 3

Views: 3941

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006944

You are creating the AsyncTask instance and executing it from a background thread. You can only create and execute an AsyncTask instance from the main application thread.

Upvotes: 8

Related Questions