1

如问题中所述..

我有一个片段,我从中创建并显示一个 DialogFragment,并将 targetFragment 设置为此当前片段。

DialogFragment_Progress fragmentProgressBar = new DialogFragment_Progress();        
    fragmentProgressBar.setTargetFragment(this, 0);
    fragmentProgressBar.show(getFragmentManager(), null);   

我做了一些进度条更新

// Thread that controls progress bar
final Thread progressThread = new Thread()
{
    @Override
    public void run()
    {           
        active = true;
        int cumulativetime = 0;

        try
        {
            while (active && cumulativetime < TIME_TOTAL)
            {
                sleep(TIME_INTERVAL);

                if (active)
                {
                    cumulativetime += TIME_INTERVAL;

                    updateProgress(cumulativetime);
                }
            }
        }
        catch (InterruptedException e)
        {

        }
        finally
        {
             onContinue();
        }
    }   
};

当进度不良完成后,我尝试调用目标 Fragment 中的方法来显示 Toast。onContinue() 代码和 targetFragment 中调用的方法如下所示。

// Perform final operations
public void onContinue()
{       
    Fragment_Activate_Program_Specific targetFragment = (Fragment_Activate_Program_Specific) getTargetFragment();

    targetFragment.operationComplete();

    dismiss();
}

操作完成。。

// Operation complete
public void operationComplete()
{       
    Toast.makeText(getActivity(), "Herp", Toast.LENGTH_SHORT).show();

    Log.i("Hello: ", "Got here!");
}

当我不尝试 Toast 时,一切都很好,收到了日志。在不涉及 UI 之前,这种方法已经为我工作了好几次。

问题:在 targetFragment() 中调用 operationComplete() 时,出现以下异常。

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

相关堆栈跟踪..

05-31 12:21:53.024: W/dalvikvm(28494): threadid=13: thread exiting with uncaught exception (group=0x40cc9930)
05-31 12:21:53.034: E/AndroidRuntime(28494): FATAL EXCEPTION: Thread-14251
05-31 12:21:53.034: E/AndroidRuntime(28494): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-31 12:21:53.034: E/AndroidRuntime(28494):    at android.os.Handler.<init>(Handler.java:197)
05-31 12:21:53.034: E/AndroidRuntime(28494):    at android.os.Handler.<init>(Handler.java:111)
05-31 12:21:53.034: E/AndroidRuntime(28494):    at android.widget.Toast$TN.<init>(Toast.java:324)
05-31 12:21:53.034: E/AndroidRuntime(28494):    at android.widget.Toast.<init>(Toast.java:91)
05-31 12:21:53.034: E/AndroidRuntime(28494):    at android.widget.Toast.makeText(Toast.java:238)
05-31 12:21:53.034: E/AndroidRuntime(28494):    at com.mypackage.Activity_Activate_Program.activateOperationComplete(Activity_Activate_Program.java:90)

我尝试使用父 Activity 而不是调用片段来托管 operationComplete() 方法,但我得到了同样的错误。

有任何想法吗?

4

2 回答 2

4

Toasts 应该从 UI 线程调用。

尝试这个。

public void operationComplete()
{       
    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getActivity(), "Herp", Toast.LENGTH_SHORT).show();
        }
    });


    Log.i("Hello: ", "Got here!");
}
于 2013-05-31T11:35:13.460 回答
1

您可以简单地覆盖对话框片段的解除方法,这只会调用一个。

@Override
Public void dismiss() {
   Log.e(TAG,"dismiss";
   super.dismiss();
 }
于 2013-05-31T11:40:31.327 回答