2012-10-19 4 views
0

커스텀 카메라를 구현했으며 일정 시간이 지나면 대기 상태가되기를 원합니다. 대기자는 stopPreview, 카메라 릴리스 및 대기 상태를 종료하기 위해 두드리기를 사용자에게 알리는보기의 내용을 포함합니다. 새 스레드 내에서 텍스트를 설정했기 때문에 CalledFromWrongThreadException이 발생하지만 솔루션이 무엇인지 알 수 없습니다. 나는 주변에 다른 게시물을 발견했지만 그들 중 누구도 실제로 일하지 않았습니다.TextView를 추가 할 때 CalledFromWrongThreadException이 발생합니다.

코드 :

private void initCamera() 
     {//more code 


       threadModifiedText = (TextView) findViewById(R.id.textView1); 
       Thread standbyThread = new Thread() 
       { 
        @Override 
        public void run() 
        { 
         try 
         { 

          while (timeCounter > 0) 
          { 
           if (!activeThread) 
           { 
            sleep(100); 
            if (timeCounter % 10 == 0) 
            { 
             threadHandler.sendEmptyMessage((int) timeCounter/10); 
            } 
            timeCounter--; 
           } 
          } 
         } 
         catch (InterruptedException e) 
         { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 

         mCamera.stopPreview(); 
         mCamera.release(); 
         TextView standbytext = new TextView(SlicesActivity.this); 
         standbytext.setText("Tap to exit standby mode"); 
         FrameLayout preview = (FrameLayout) findViewById(id.FrameLayout_camera_preview); 
         preview.addView(standbytext); 

        } 
       }; 
       standbyThread.start(); 

//more code} 

그리고

@Override 
    public void onUserInteraction() 
    { 
     Log.d("~~~~~~~~~", "apasat"); 
     activeThread = true; 
      timerCounter = 300; 


    } 

    private Handler threadHandler = new Handler() 
    { 
     public void handleMessage(android.os.Message msg) 
     { 
      // whenever the Thread notifies this handler we have 
      // only this behavior 
      threadModifiedText.setText("\ncounter is " + Integer.toString(msg.what)); 
     } 
    }; 

하시기 바랍니다 사람이 나에게 몇 가지 제안을 제공합니다. 10 배

+2

스레드에서 standbytext를 설정할 수 없습니다. UIThread에서 수행해야합니다. –

답변

1

시도는 UI

당신은 UI 스레드를 제외한 모든 스레드 내부의 텍스트 값을 설정할 수 없습니다
1

을 업데이트 스레드에서

runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      // TODO: update your UI here 
     } 
    }); 

를 사용합니다. UI 스레드에서 작업이 끝나면 백그라운드 스레드 작업을 완료하고 텍스트 값을 설정하십시오.

+0

답변을 승인으로 표시하십시오. 감사 –

관련 문제