注意:已经访问了this链接,但没有帮助。
在运行Robolectric Junit测试时,我收到了由
引起的StackOverFlow错误ProgressDialog.show()
在AsyncTask内部进行调用。
我有以下类型的活动:
public abstract class TransactionFragmentActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transction_layout);
SetUpService setup = new SetUpService();
setup.execute();
}
private class SetUpService extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog = new ProgressDialog(TransactionFragmentActivity.this);
/** progress dialog to show user that the backup is processing. */
/** application context. */
@Override
protected void onPreExecute() {
SetUpService.this.dialog.setMessage("Please wait");
SetUpService.this.dialog.setCancelable(false);
SetUpService.this.dialog.show();
}
@Override
protected Boolean doInBackground(final String... args) {
// Setting data
Log.d(TAG,"doInBackground...");
fetchData(); //some method doing work
return true;
}
@Override
protected void onPostExecute(final Boolean success) {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
}
}
以下是stacktrace:
java.lang.StackOverflowError的 at java.lang.Class.getConstructor0(Unknown Source) at java.lang.Class.getDeclaredConstructor(Unknown Source) at org.robolectric.bytecode.RobolectricInternals.newInstance(RobolectricInternals.java:39) 在org.robolectric.bytecode.RobolectricInternals.directlyOn(RobolectricInternals.java:54) 在org.robolectric.Robolectric.directlyOn(Robolectric.java:366) 在org.robolectric.shadows.ShadowView.directly(ShadowView.java:439) 在org.robolectric.shadows.ShadowView.invalidate(ShadowView.java:168) 在android.view.View.invalidate(View.java) 在android.widget.TextView.setPadding(TextView.java:2122) 在android.view.View.recomputePadding(View.java:10914) 在android.view.View.resolvePadding(View.java:11584) 在android.view.View.resolveLayoutDirection(View.java:11524) 在android.view.View.onAttachedToWindow(View.java:11440) 在android.widget.TextView.onAttachedToWindow(TextView.java:4448) 在android.view.View.dispatchAttachedToWindow(View.java:11755) 在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2424) 在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2424) 在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2424) 在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2424) 在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2424) 在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2424) 在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2424) 在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2424) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1201) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4214) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:725) 在android.view.Choreographer.doCallbacks(Choreographer.java:555) 在android.view.Choreographer.doFrame(Choreographer.java:525) 在android.view.Choreographer $ FrameHandler.handleMessage(Choreographer.java:657) 在org.robolectric.shadows.ShadowHandler.routeMessage(ShadowHandler.java:125) 在org.robolectric.shadows.ShadowHandler.access $ 100(ShadowHandler.java:25) 在org.robolectric.shadows.ShadowHandler $ 1.run(ShadowHandler.java:110) 在org.robolectric.util.Scheduler.postDelayed(Scheduler.java:37) 在org.robolectric.shadows.ShadowLooper.post(ShadowLooper.java:198) 在org.robolectric.shadows.ShadowHandler.postDelayed(ShadowHandler.java:56) 在org.robolectric.shadows.ShadowHandler.sendMessageDelayed(ShadowHandler.java:105) 在org.robolectric.shadows.ShadowHandler.sendMessageAtTime(ShadowHandler.java:160) 在android.os.Handler.sendMessageAtTime(Handler.java) 在android.view.Choreographer.scheduleFrameLocked(Choreographer.java:479) 在android.view.Choreographer.postCallbackDelayedInternal(Choreographer.java:309) 在android.view.Choreographer.postCallbackDelayed(Choreographer.java:292) 在android.view.Choreographer.postCallback(Choreographer.java:266) 在android.view.ViewRootImpl.scheduleTraversals(ViewRootImpl.java:974) 在android.view.ViewRootImpl.setLayoutParams(ViewRootImpl.java:792) 在android.view.WindowManagerImpl.updateViewLayout(WindowManagerImpl.java:343) 在android.app.Activity.onWindowAttributesChanged(Activity.java:2267) 在android.view.Window.setDefaultWindowFormat(Window.java:1226) 在com.android.internal.policy.impl.PhoneWindow.access $ 1300(PhoneWindow.java:105) 在com.android.internal.policy.impl.PhoneWindow $ DecorView.drawableChanged(PhoneWindow.java:2407) 在com.android.internal.policy.impl.PhoneWindow $ DecorView.fitSystemWindows(PhoneWindow.java:2345) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1203) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4214) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:725) 在android.view.Choreographer.doCallbacks(Choreographer.java:555) 在android.view.Choreographer.doFrame(Choreographer.java:525) 在android.view.Choreographer $ FrameHandler.handleMessage(Choreographer.java:657) 在org.robolectric.shadows.ShadowHandler.routeMessage(ShadowHandler.java:125) 在org.robolectric.shadows.ShadowHandler.access $ 100(ShadowHandler.java:25) 在org.robolectric.shadows.ShadowHandler $ 1.run(ShadowHandler.java:110) 在org.robolectric.util.Scheduler.postDelayed(Scheduler.java:37) 在org.robolectric.shadows.ShadowLooper.post(ShadowLooper.java:198) 在org.robolectric.shadows.ShadowHandler.postDelayed(ShadowHandler.java:56) 在org.robolectric.shadows.ShadowHandler.sendMessageDelayed(ShadowHandler.java:105) 在org.robolectric.shadows.ShadowHandler.sendMessageAtTime(ShadowHandler.java:160) 在android.os.Handler.sendMessageAtTime(Handler.java) 在android.view.Choreographer.scheduleFrameLocked(Choreographer.java:479) 在android.view.Choreographer.postCallbackDelayedInternal(Choreographer.java:309) 在android.view.Choreographer.postCallbackDelayed(Choreographer.java:292) 在android.view.Choreographer.postCallback(Choreographer.java:266) 在android.view.ViewRootImpl.scheduleTraversals(ViewRootImpl.java:974) 在android.view.ViewRootImpl.setLayoutParams(ViewRootImpl.java:792) 在android.view.WindowManagerImpl.updateViewLayout(WindowManagerImpl.java:343) 在android.app.Activity.onWindowAttributesChanged(Activity.java:2267) 在android.view.Window.setDefaultWindowFormat(Window.java:1226) 在com.android.internal.policy.impl.PhoneWindow.access $ 1300(PhoneWindow.java:105) 在com.android.internal.policy.impl.PhoneWindow $ DecorView.drawableChanged(PhoneWindow.java:2407) 在com.android.internal.policy.impl.PhoneWindow $ DecorView.fitSystemWindows(PhoneWindow.java:2345) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1203) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4214) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:725) 在android.view.Choreographer.doCallbacks(Choreographer.java:555) 在android.view.Choreographer.doFrame(Choreographer.java:525) 在android.view.Choreographer $ FrameHandler.handleMessage(Choreographer.java:657) 在org.robolectric.shadows.ShadowHandler.routeMessage(ShadowHandler.java:125) 在org.robolectric.shadows.ShadowHandler.access $ 100(ShadowHandler.java:25) 在org.robolectric.shadows.ShadowHandler $ 1.run(ShadowHandler.java:110) 在org.robolectric.util.Scheduler.postDelayed(Scheduler.java:37) 在org.robolectric.shadows.ShadowLooper.post(ShadowLooper.java:198) 在org.robolectric.shadows.ShadowHandler.postDelayed(ShadowHandler.java:56) 在org.robolectric.shadows.ShadowHandler.sendMessageDelayed(ShadowHandler.java:105) 在org.robolectric.shadows.ShadowHandler.sendMessageAtTime(ShadowHandler.java:160) 在android.os.Handler.sendMessageAtTime(Handler.java) 在android.view.Choreographer.scheduleFrameLocked(Choreographer.java:479) 在............
答案 0 :(得分:0)
尝试final ProgressDialog dialog = new ProgressDialog(getApplicationContext);
答案 1 :(得分:0)
private ProgressDialog dialog = new ProgressDialog(getActivity());
protected void onPreExecute() {
dialog.setMessage("Please wait");
dialog.setCancelable(false);
dialog.show();
}
或
Activity act = (TransactionFragmentActivity) this;
private ProgressDialog dialog = new ProgressDialog(act);
protected void onPreExecute() {
dialog.setMessage("Please wait");
dialog.setCancelable(false);
dialog.show();
}
认为它会起作用。看看,告诉我。