2015-11-03 85 views
1

我的警報對話框彈出27次。我有實現處理程序和處理程序內,我使用CountDownTimer。警報對話框代碼位於CountDownTimer的onFinish上。以下是我的代碼。警報對話框在處理程序中彈出27次

public void onCreate(Bundle savedInstanceState) { 





    Log.i(TAG, "called onCreate"); 
    super.onCreate(savedInstanceState); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

    setContentView(R.layout.face_detect_surface_view); 

    mOpenCvCameraView = (Tutorial3View) findViewById(R.id.tutorial3_activity_java_surface_view); 

    mOpenCvCameraView.setCvCameraViewListener(this); 



    mPath=getFilesDir()+"/facerecogOCV/"; 

    labelsFile= new labels(mPath); 

    Iv=(ImageView)findViewById(R.id.imageView1); 
    textresult = (TextView) findViewById(R.id.textView1); 

    mHandler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      Log.e("X", (String) msg.obj); 

      if (msg.obj=="IMG") 
      { 
      Canvas canvas = new Canvas(); 
      canvas.setBitmap(mBitmap); 
      Iv.setImageBitmap(mBitmap); 
      if (countImages>=MAXIMG-1) 
      { 
       toggleButtonGrabar.setChecked(false); 
       grabarOnclick(); 
      } 
      } 
      else 
      { 
       textresult.setText(msg.obj.toString()); 

       textpercent.setVisibility(View.VISIBLE); 
       textpercent.setText(mLikely + "%"); 


       new CountDownTimer(5000, 1000) { 

        @Override 
        public void onTick(long millisUntilFinished) { 
         // TODO Auto-generated method stub 
         mOpenCvCameraView.enableView(); 
        } 

        @Override 
        public void onFinish() { 
         // TODO Auto-generated method stub 
         final AlertDialog ad = new AlertDialog.Builder(FdActivity.this).create(); 
         ad.setTitle("Recognition Status"); 
         ad.setMessage("Criminal ID :" + textresult.getText().toString() + "\nSimilarities : " + textpercent.getText().toString() ); 
         ad.setButton("Rescan", new DialogInterface.OnClickListener() { 

          public void onClick(DialogInterface dialog, int which) { 


           Toast.makeText(getApplicationContext(), 
             "Rescanning", Toast.LENGTH_SHORT).show(); 
           Bundle configBundle = new Bundle(); 
           onCreate(configBundle); 
           mOpenCvCameraView.enableView(); 



          } 
         }); 




         ad.show();//pops out 27 times 

         mOpenCvCameraView.disableView(); 
         buttonSearch.setChecked(false); 


        } 
       }.start(); 

      } 

     } 
    }; 
    text=(EditText)findViewById(R.id.editText1); 
    buttonCatalog=(Button)findViewById(R.id.buttonCat); 
    toggleButtonGrabar=(ToggleButton)findViewById(R.id.toggleButtonGrabar); 
    buttonSearch=(ToggleButton)findViewById(R.id.buttonBuscar); 
    toggleButtonTrain=(ToggleButton)findViewById(R.id.toggleButton1); 
    textState= (TextView)findViewById(R.id.textViewState);  
    textpercent = (TextView)findViewById(R.id.textViewpercent); 
    imCamera=(ImageButton)findViewById(R.id.imageButton1); 
    textpercent.setVisibility(View.INVISIBLE);  
     text.setVisibility(View.INVISIBLE); 
     textresult.setVisibility(View.INVISIBLE);  
     toggleButtonGrabar.setVisibility(View.INVISIBLE);  
     text.setOnKeyListener(new View.OnKeyListener() { 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 
       if ((text.getText().toString().length()>0)&&(toggleButtonTrain.isChecked())) 
        toggleButtonGrabar.setVisibility(View.VISIBLE); 
       else 
        toggleButtonGrabar.setVisibility(View.INVISIBLE); 

      return false; 
     } 
    }); 

    toggleButtonTrain.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      if (toggleButtonTrain.isChecked()) { 
       textState.setText(getResources().getString(R.string.SEnter)); 
       buttonSearch.setVisibility(View.INVISIBLE); 
       textresult.setVisibility(View.VISIBLE); 
       text.setVisibility(View.VISIBLE); 
      textresult.setText(getResources().getString(R.string.SFaceName)); 
       if (text.getText().toString().length() > 0) 
        toggleButtonGrabar.setVisibility(View.VISIBLE); 
       textpercent.setVisibility(View.INVISIBLE); 


      } else { 
       textState.setText(R.string.Straininig); 
       textresult.setText(""); 
       text.setVisibility(View.INVISIBLE); 

       buttonSearch.setVisibility(View.VISIBLE); 
       ; 
       textresult.setText(""); 
       { 
        toggleButtonGrabar.setVisibility(View.INVISIBLE); 
        text.setVisibility(View.INVISIBLE); 
       } 
       Toast.makeText(getApplicationContext(), getResources().getString(R.string.Straininig), Toast.LENGTH_LONG).show(); 
       fr.train(); 
       textState.setText(getResources().getString(R.string.BLANK)); 

      } 
     } 

    }); 
    toggleButtonGrabar.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      grabarOnclick(); 
     } 
    }); 

    imCamera.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      if (mChooseCamera==frontCam) 
      { 
       mChooseCamera=backCam; 
       mOpenCvCameraView.setCamBack(); 
      } 
      else 
      { 
       mChooseCamera=frontCam; 
       mOpenCvCameraView.setCamFront(); 

      } 
     } 
    }); 

    buttonSearch.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       if (buttonSearch.isChecked()) 
       { 
        if (!fr.canPredict()) 
         { 
         buttonSearch.setChecked(false); 
         Toast.makeText(getApplicationContext(), getResources().getString(R.string.SCanntoPredic), Toast.LENGTH_LONG).show(); 
         return; 
         } 
        textState.setText(getResources().getString(R.string.SSearching)); 
        toggleButtonGrabar.setVisibility(View.INVISIBLE); 
        toggleButtonTrain.setVisibility(View.INVISIBLE); 
        text.setVisibility(View.INVISIBLE); 
        faceState=SEARCHING; 
        textresult.setVisibility(View.VISIBLE); 
       } 
       else 
       { 
        faceState=IDLE; 
        textState.setText(getResources().getString(R.string.SIdle)); 
        toggleButtonGrabar.setVisibility(View.INVISIBLE); 
        toggleButtonTrain.setVisibility(View.VISIBLE); 
        text.setVisibility(View.INVISIBLE); 
        textresult.setVisibility(View.INVISIBLE); 

       } 
      } 
     }); 

    boolean success=(new File(mPath)).mkdirs(); 
    if (!success) 
    { 
     Log.e("Error", "Error creating directory"); 
    } 
} 

有人請幫我解決這個問題。我仍然是Android編程的學習者。我不知道如何解決這個問題。

謝謝

+0

也許mHandler調用'public void handleMessage(Message msg)'27次? – walkmn

+0

我應該刪除它嗎? –

+0

不,請檢查你的其他代碼。 mHandler在哪裏使用和調用? – walkmn

回答

0

我認爲你的處理程序獲取消息的數量。你可以使用

Log.e("X", msg.obj); 

來驗證,並檢查該輸出的logcat。

+0

我在哪裏粘貼? –

+0

就在此行'textresult.setText(msg.obj.toString());' – LilaQ

+0

11-04 00:49:31.789 9487-9487/com.example.arafat.portablefacerecognitionsystem E/X:h 11-04 00 :49:31.794 9487-10308/com.example.arafat.portablefacerecognitionsystem D/CameraBridge:mStretch value:1.0416666 11-04 00:49:31.807 9487-9487/com.example.arafat.portablefacerecognitionsystem D/JavaCameraView:Preview收到的幀。框架大小:622080 –