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(); を実行することをハンドラーに伝える方法はありますか? ?

または、私がいつ電話したかを知る方法がありますか(何か赤いものに触れたとき)

   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);



                  }

すべてのハンドラー キューを終了するには?

私は探していましたが、アプリケーションを終了する前に、アプリケーション内のすべてのハンドラーを強制終了する方法が見つかりません。

少し押し込んでいることは知っていますが、ゲームをテストするとクラッシュする場合があります。もちろん、通常のプレイではクラッシュしないかもしれません。ポップアップが表示されてから数ミリ秒ですが、そのエラーが手元にあるのが気になります。

他のスレッドでの回答のおかげで、Android のプログラミングについて最も多くのことを学びました。ありがとうございます。

これは修正されたコードです サムに感謝します ! :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 への参照を保存する 2 つのフィールド変数を作成します。

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 に答える