首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓:你的活动在运行吗?对话框无法在OnFinish上显示

安卓:你的活动在运行吗?对话框无法在OnFinish上显示
EN

Stack Overflow用户
提问于 2013-11-21 16:38:14
回答 1查看 1.8K关注 0票数 1

我有一个名为SpotOnView的视图,正在Game_shooting_AB上扩展。编码如下。

背景:

在SpotOnView中有一个倒计时器。当时间到了,游戏就结束了,会弹出一个对话框来询问用户是想退出还是再玩一次。

SpotOnView代码:

代码语言:javascript
复制
public class SpotOnView extends View 
{
   ......
  // constructs a new SpotOnView
   public SpotOnView(Context context, RelativeLayout parentLayout)
   {
      super(context);          
      resources = context.getResources();            
      layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      ....    

   } 


   public void start_timer() 
   {
        if (!timerHasStarted) 
        {
            countDownTimer.start();
            timerHasStarted = true;
        } 
        else 
        {
            countDownTimer.cancel();
            timerHasStarted = false;
        }
   }

    public class MyCountDownTimer extends CountDownTimer 
    {
        public MyCountDownTimer(long startTime, long interval) 
        {
            super(startTime, interval);
        }

        @Override
        public void onFinish() 
        {
            text.setText("Time's up!");
               ((Game_shooting_AB)getContext()).replay_dialog();  //LINE 181
        }

        @Override
        public void onTick(long millisUntilFinished) 
        {
            ....
        }
    }

Game_shooting_AB代码:

代码语言:javascript
复制
   public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.game_shooting);

      // create a new SpotOnView and add it to the RelativeLayout
      RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativeLayout);
      view = new SpotOnView(this, layout);                                          //EXTENDING HERE
      layout.addView(view, 0); // add view to the layout
    ....
}



public void replay_dialog()    
{
    final Dialog dialog1 = new Dialog(Game_shooting_AB.this, android.R.style.Theme_Translucent_NoTitleBar);
    WindowManager.LayoutParams lp = dialog1.getWindow().getAttributes();
    lp.dimAmount = 0.7f;
    dialog1.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    Window window = dialog1.getWindow();
    window.setGravity(Gravity.CENTER);

    dialog1.setContentView(R.layout.alert_dialog_replay_shooting);
    dialog1.setCancelable(false);
    dialog1.show();     //LINE 431
    ....

逻辑猫:

在上述代码中添加了必要的行号作为注释。

代码语言:javascript
复制
11-22 00:16:34.365: W/dalvikvm(1105): threadid=1: thread exiting with uncaught exception (group=0x41f342a0)
11-22 00:16:34.370: E/AndroidRuntime(1105): FATAL EXCEPTION: main
11-22 00:16:34.370: E/AndroidRuntime(1105): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@43304558 is not valid; is your activity running?
11-22 00:16:34.370: E/AndroidRuntime(1105):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:708)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:346)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at android.view.Window$LocalWindowManager.addView(Window.java:554)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at android.app.Dialog.show(Dialog.java:277)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at com.app.abc.Game_shooting_AB.replay_dialog(Game_shooting_AB.java:431)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at com.app.abc.SpotOnView$MyCountDownTimer.onFinish(SpotOnView.java:181)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at android.os.Looper.loop(Looper.java:137)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at android.app.ActivityThread.main(ActivityThread.java:4898)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at java.lang.reflect.Method.invokeNative(Native Method)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at java.lang.reflect.Method.invoke(Method.java:511)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
11-22 00:16:34.370: E/AndroidRuntime(1105):     at dalvik.system.NativeStart.main(Native Method)

问题:

logcat显示您的活动正在运行吗?

  1. 我知道对话需要有一个基础才能显示出来。但是如何修改代码呢?
  2. 是否可以在SpotOnView中编码和显示对话框,而不是在Game_shooting_AB代码中编写?
  3. 我试图通过将类MyCountDownTimer和方法replay_dialog移回Game_shooting_AB来修改代码,但同样的错误仍然出现。我想问一下,OnFinish of MyCountDownTimer是否会使活动完成,这样对话框就无法找到要显示的基础?

太感谢了!!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-21 17:19:14

问题是从"((Game_shooting_AB)getContext()).replay_dialog();“开始的--这一行代码。

当您从视图内部调用getContext()时,您将从android的文档中找到“视图的上下文”。现在您要做的是将该上下文转换为Game_shooting_AB,这并不好,因为这可能是活动的上下文,也可能不是活动的上下文。请看Romain Guy's answer关于这一点。因此,android告诉你“你的活动在运行吗?”

现在,要解决当前的问题,可以通过任何公共方法将活动实例传递给此视图,并将其存储在局部变量中。

按照以下方式调整代码:

代码语言:javascript
复制
public class SpotOnView extends View 
{
  Game_shooting_AB mGame_shooting_AB;
  public void setGame_shooting_AB(Game_shooting_AB mGame_shooting_AB){
   this.mGame_shooting_AB = mGame_shooting_AB;
  }
   ......
   // constructs a new SpotOnView
  public SpotOnView(Context context, RelativeLayout parentLayout)
  {
   super(context);          
   resources = context.getResources();            
    layoutInflater = (LayoutInflater)          context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  ....    

  } 


  public void start_timer() 
  {
    if (!timerHasStarted) 
    {
        countDownTimer.start();
        timerHasStarted = true;
    } 
    else 
    {
        countDownTimer.cancel();
        timerHasStarted = false;
    }
  }

public class MyCountDownTimer extends CountDownTimer 
{
    public MyCountDownTimer(long startTime, long interval) 
    {
        super(startTime, interval);
    }

    @Override
    public void onFinish() 
    {
        text.setText("Time's up!");
           mGame_shooting_AB.replay_dialog();  //LINE 181
    }

    @Override
    public void onTick(long millisUntilFinished) 
    {
        ....
    }
}

在获得SpotOnView实例之后,设置活动:

代码语言:javascript
复制
      view = new SpotOnView(this, layout);                                          //EXTENDING HERE
  layout.addView(view, 0); // add view to the layout
  view.setGame_shooting_AB(Game_shooting_AB.this);

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20126762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档