无法在线程内创建处理程序 - Android

时间:2015-07-18 09:49:14

标签: android multithreading android-activity

我从我的android日志中收到此错误,我已经查看了其他有这种问题的线程,但我仍然无法修复自己的问题

基本上,我想通过回调机制

从服务类更新我的UI
//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;


    }
}

然后我从处理器类(从服务类调用)

调用此语句
callback.updateProgress(Constants.ProgressUIPostUser.PD_OPEN_CONNECTION);

来自服务类

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

记录错误

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)

3 个答案:

答案 0 :(得分:1)

您正在从非UI线程调用PostNewUserProcessor processor = new PostNewUserProcessor(mCallback);

但是应该在UI线程中显示一个对话框。喜欢这个..

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

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

            }
        });
            break;

答案 1 :(得分:0)

添加

Looper.prepare();

在你的线程内作为第一个语句。

阅读以下内容,了解有关Looper线程的更多信息:

http://www.oracle.com/technetwork/java/javaee/downloads/index.html

答案 2 :(得分:0)