1

这是我有一个处理程序,当我触摸背景颜色为白色的区域时,它会在屏幕上显示一条消息。

                  else if (ct.closeMatch (Color.WHITE, touchColor, tolerance))             

                               {  Random r = new Random();
                           int txt= r.nextInt(6-0) + 0;
                           if(txt==0){ variables.pointtxt = "Nothing interesting"; }
                           else if (txt==1){ variables.pointtxt = "There´s nothing there"; }     
                           else if (txt==2){ variables.pointtxt = "I can´t do nothing with that"; } 
                           else if (txt==3){ variables.pointtxt = "Wait... nop nothing"; }  
                           else if (txt==4){ variables.pointtxt = "Nothing"; }  
                           else if (txt==5){ variables.pointtxt = "More nothing"; }
                                LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                                  View popupView = layoutInflater.inflate(R.layout.popup, null);  
                                       final PopupWindow popupWindow = new PopupWindow(
                                          popupView, 
                                          LayoutParams.FILL_PARENT,  
                                                 LayoutParams.WRAP_CONTENT);
                                          TextView text = (TextView) popupView.findViewById(R.id.popuptxt);
                                        text.setText(variables.pointtxt);

                                        popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 250) ;




                                new Handler().postDelayed(new Runnable()
                                {
                                    public void run()
                                      {      if (popupWindow.isShowing()== true)

                                         popupWindow.dismiss();

                                      }
                                }, 1000);




                                   }

但是,如果我在不到 1000 毫秒的时间内运行一个新意图,它就会崩溃,我很确定,因为它可以完成处理程序提示。

有没有办法告诉处理程序如果应用程序正在关闭然后运行 ​​popupWindow.dismiss(); ?

或 inted 有没有办法告诉我什么时候打电话(触摸红色的东西时)

   if (ct.closeMatch (Color.RED, touchColor, tolerance)) {


                       Intent game = new Intent(lvl2_1_0.this, lvl2_1_1.class); 
                       game.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                          game.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                        startActivity(game);



                  }

完成所有处理程序提示?

我一直在寻找,但我找不到一种方法来强制完成应用程序中的所有处理程序,然后再完成它。

我知道我可能会稍微推动它,但在某些情况下,当我测试游戏时它会崩溃,当然在正常游戏中它可能不会发生,因为按下按钮在不到 1000 秒内通过屏幕是不可能的弹出弹出窗口后的毫秒数但让我感到困扰的是我手上有这个错误。

感谢您在其他线程中的回答,我学到了有关 android 编程的最多知识,非常感谢!

这是已修复的代码,感谢 sam!:P

public class lvl2_1_0 extends Activity implements View.OnTouchListener {
private PopupWindow popupWindow;
private Handler mHandler = new Handler();
private Runnable dismissPopup = new Runnable() {
        public void run() {
            if (popupWindow.isShowing())
                popupWindow.dismiss();
        }
    };
   @Override
    public void onCreate(Bundle savedInstanceState) {

然后

调用弹出窗口。就我而言:

   LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                                  View popupView = layoutInflater.inflate(R.layout.popup, null);  
                                  popupWindow = new PopupWindow(
                                          popupView, 
                                          LayoutParams.FILL_PARENT,  
                                                 LayoutParams.WRAP_CONTENT);
                                          TextView text = (TextView) popupView.findViewById(R.id.popuptxt);
                                        text.setText(variables.pointtxt);

                                        popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 250) ;



                                        mHandler.postDelayed(dismissPopup, 3000);

最后在没有强制关闭的情况下关闭应用程序

                       Intent game = new Intent(lvl2_1_0.this, lvl2_1_1.class); 
                       game.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                          game.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                          mHandler.removeCallbacks(dismissPopup);
                        startActivity(game);
4

1 回答 1

1

但是,如果我在不到 1000 毫秒的时间内运行一个新意图,它就会崩溃,我很确定,因为它可以完成处理程序提示。

Handler#removeCallbacks(Runnable)将取消它在队列中等待的传递的 Runnable。

创建两个字段变量以保存对 Handler 和 Runnable 的引用:

public class Example extends Activity {
    private Handler mHandler = new Handler();
    private Runnable dismissPopup = new Runnable() {
        public void run() {
            if (popupWindow.isShowing())
                popupWindow.dismiss();
        }
    }

现在您之前调用new Handler().postDelayed(new R...使用:

mHandler.postDelayed(dismissPopup, 1000);

最后在你启动新的 Intent 调用之前:

...
mHandler.removeCallbacks(mRunnable);`
startActivity(game);  

(你可能也想打电话removeCallbacks()onPause()

于 2012-12-02T07:06:42.800 回答