Reputation:
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)
Upvotes: 0
Views: 722
Reputation: 3083
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
Upvotes: 0
Reputation: 4884
Just Add
Looper.prepare();
inside your thread as the first statement.
Learn more about Looper threads by reading :
Upvotes: 0
Reputation: 2806
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;
Upvotes: 1