0

I am getting this error from my android log and I have looked to other threads that have this kind of problem but i still cannot fix my own problem

Basically, i would like to update my UI from service class by callback mechanism

//this method is inside the fragment class
public void updateProgress(int progress){
    switch(progress){
        case Constants.ProgressUIPostUser.PD_VALIDATE_USER:
            pdPostUserLoading = ProgressDialog.show(getActivity(), "Registration", "Validating user...", true, false);
            break;
        case Constants.ProgressUIPostUser.PD_POSTING_DATA:
            pdPostUserLoading = ProgressDialog.show(getActivity(), "Registration", "Posting data...", true, false);
            break;
        case Constants.ProgressUIPostUser.PD_OPEN_CONNECTION:
            pdPostUserLoading = ProgressDialog.show(getActivity(), "Registration", "Open connection...", true, false);
            break;
        case Constants.ProgressUIPostUser.PD_OBTAINING_RESPONSE:
            pdPostUserLoading = ProgressDialog.show(getActivity(), "Registration", "Obtaining Response...", true, false);
            break;
        case Constants.ProgressUIPostUser.PD_DISMISS:
            if(pdPostUserLoading != null)
                pdPostUserLoading.dismiss();
            break;


    }
}

Then i called this statement from processor class (which is called from the service class)

callback.updateProgress(Constants.ProgressUIPostUser.PD_OPEN_CONNECTION);

From the service class

public interface Callback {
    void updateProgress(int progress);
}

class Task implements  Runnable{
    public void run(){
        PostNewUserProcessor processor  = new PostNewUserProcessor(mCallback);
        boolean valid = processor.postUser(newUser);

        if(valid){
            //Todo
        }
        else{
            //TODO
        }
    }

public void postUser(Bundle data){

        //Retrieve data from the intent
    newUser = data.getParcelable(Constants.NEW_USER);

    new Thread(new Task());
}

Log error

07-18 18:52:44.994    8942-9612/? E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-1137
    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
            at android.os.Handler.<init>(Handler.java:197)
            at android.os.Handler.<init>(Handler.java:111)
            at android.app.Dialog.<init>(Dialog.java:108)
            at android.app.AlertDialog.<init>(AlertDialog.java:114)
            at android.app.AlertDialog.<init>(AlertDialog.java:98)
            at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
            at android.app.ProgressDialog.show(ProgressDialog.java:110)
            at android.app.ProgressDialog.show(ProgressDialog.java:104)
            at com.robertlimantoproject.bookreviewapp.activity.RegistrationPageFragment.updateProgress(RegistrationPageFragment.java:354)
            at com.robertlimantoproject.bookreviewapp.processor.PostNewUserProcessor.postUser(PostNewUserProcessor.java:81)
            at com.robertlimantoproject.bookreviewapp.service.PostNewUserService$Task.run(PostNewUserService.java:61)
            at com.robertlimantoproject.bookreviewapp.utils.Util$1.run(Util.java:36)
Robert Limanto
  • 2,068
  • 4
  • 17
  • 27

3 Answers3

1

You are calling PostNewUserProcessor processor = new PostNewUserProcessor(mCallback); from a non-UI thread.

But showing a dialog should be written in the UI thread. like this..

 case Constants.ProgressUIPostUser.PD_VALIDATE_USER:
           getActivity().runOnUiThread(new Runnable() {

            @Override
            public void run() {
                pdPostUserLoading = ProgressDialog.show(getActivity(), "Registration", "Validating user...", true, false);

            }
        });
            break;
Emil
  • 2,786
  • 20
  • 24
  • Thanks a lot, it works!!! i still do not know why it is not in the main by default since i have put the method inside the activity-fragment which suppose to be the main thread? could you explain to me? – Robert Limanto Jul 18 '15 at 10:21
  • How do i put this as an answer in my post? – Robert Limanto Jul 18 '15 at 10:24
  • As i said you are passing the callback **PostNewUserProcessor processor = new PostNewUserProcessor(mCallback);** in a non UI thread. Then the callback method **updateProgress()** will work in the same thread where it is called from. i. e the non UI thread. – Emil Jul 18 '15 at 10:28
  • To mark an answer as accepted, click on the check mark beside the answer to toggle it from hollow to green – Emil Jul 18 '15 at 10:31
  • Thanks for the answer bro :D – Robert Limanto Jul 18 '15 at 16:33
0

Just Add

Looper.prepare();

inside your thread as the first statement.

Learn more about Looper threads by reading :

Looper Thread

Dr. Ehsan Ali
  • 4,735
  • 4
  • 23
  • 37
0

As the error says, you forgot to add looper.prepare inside your thread.
Take a look here:
Can't create handler inside thread which has not called Looper.prepare() and
Can't create handler inside thread that has not called Looper.prepare() in android.
Next, in order to implement what you want take a look here:
Can't see ProgressDialog while AsyncTask run in background
Tutorial:
http://www.41post.com/4588/programming/android-coding-a-loading-screen-part-1

Community
  • 1
  • 1
Andrei T
  • 2,985
  • 3
  • 21
  • 28