正如問題所述..Android - DialogFragment從片段中顯示。當DialogFragment被解僱時如何從片段中顯示Toast?
我有一個片段,我創建並顯示一個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();
}
}
};
當壞進度完成時,我嘗試調用一個方法在目標片段顯示吐司。該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!");
}
當我不嘗試和烤麪包片,一切都很好,在記錄已收到。在沒有涉及UI的情況下,這種方法已經爲我多次工作了。
問題:當operationComplete()在targetFragment()被調用時,我得到下面的異常..
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)
我已經嘗試使用父活動,而不是調用片段來承載operationComplete()方法,但我得到相同的錯誤。
任何想法?
完美!感謝您及時的回覆 – mgibson