2013-06-27 1 views
-1

혼란을 드려 죄송합니다. Worng Logcat 정보를 게시했습니다. 나는 그 질문을 갱신했다. 시작을 클릭하여 스레드를 시작한 다음 입력을 클릭 할 때 thad가 계속 진행되도록하고 스레드에서 메시지를 처리 ​​한 다음 주 스레드로 출력하고 텍스트보기를 업데이트하려고합니다. 어떻게 입력을 기다리고 핸들러에 대한 번들을 얻을 스레드를 시작하겠습니까?wait() 구현 방법; notifyAll();을 기다린다. 입력 버튼에서?

다음
public class MainActivity extends Activity implements OnClickListener { 
Handler mHandler; 
Button enter; 
Button start; 
TextView display; 
String dateString; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    enter = (Button) findViewById(R.id.enter); 
    start = (Button) findViewById(R.id.start); 
    display = (TextView) findViewById(R.id.Display); 
    enter.setOnClickListener(this); 
    start.setOnClickListener(this); 

    mHandler = new Handler() { <=============================This is Line 31 
     public void handleMessage(Message msg) { 
      // TODO Auto-generated method stub 
      super.handleMessage(msg); 
      Bundle bundle = msg.getData(); 
      String string = bundle.getString("outKey"); 
      display.setText(string); 

     } 
    }; 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.enter: 
     Message msgin = Message.obtain(); 
     Bundle bundlein = new Bundle(); 
     String in = "It Works!"; 
     bundlein.putString("inKey", in); 
     msgin.setData(bundlein); 
     notifyAll(); 

     break; 
    case R.id.start: 
     new myThread().hello.start(); 
     break; 
    } 
} 

public class myThread extends Thread { 
    Thread hello = new Thread() { 
     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      super.run(); 
      Looper.prepare(); 
      try { 
       wait(); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      Handler Mhandler = new Handler() { 

       @Override 
       public void handleMessage(Message msg) { 
        // TODO Auto-generated method stub 
        super.handleMessage(msg); 
        Bundle bundle = msg.getData(); 
        dateString = bundle.getString("inKey"); 
       } 

      }; 
      Looper.loop(); 

      Message msg = Message.obtain(); 
      Bundle bundle = new Bundle(); 

      bundle.putString("outKey", dateString); 
      msg.setData(bundle); 
      mHandler.sendMessage(msg); 

     } 

    }; 

} 
} 

가 로그 캣 정보입니다 : 다음은 내 코드입니다

06-27 00:00:24.832: E/AndroidRuntime(18513): FATAL EXCEPTION: Thread-1210 
06-27 00:00:24.832: E/AndroidRuntime(18513): java.lang.IllegalMonitorStateException: object not locked by thread before wait() 
06-27 00:00:24.832: E/AndroidRuntime(18513): at java.lang.Object.wait(Native Method) 
06-27 00:00:24.832: E/AndroidRuntime(18513): at java.lang.Object.wait(Object.java:364) 
06-27 00:00:24.832: E/AndroidRuntime(18513): at com 
.example.learninghandlers.MainActivity$myThread$1.run(MainActivity.java:77) 
+0

logcat 대신 app의 stacktrace를 게시하는 것이 좋습니다. –

+0

당신의'onCreate()'메쏘드에 들어간 것 같습니다. 줄 번호로, 나는 더 구체적 일 수있다. 아이디가 잘못되었거나 사용하기 전에 레이아웃을 부 풀리지 않을 가능성이 있습니다. –

+1

31 번 줄은 어느 쪽입니까? –

답변

0

본 게시물시 현재 오류,

java.lang.IllegalMonitorStateException: object not locked by thread before wait() 

가 발생합니다().

모니터를 소유하려면이 클래스의 개체와 동기화 된 코드 블록 안에 있거나 클래스의 동기화 된 메서드 내부에 있어야합니다.

그러나 스레드의 전반적인 사용은 다소 불규칙 해 보이며이 문제의 뒤에 숨어있는 추가 문제가있을 수 있습니다.

+0

Chris 고마워요.이 코딩은 오히려 나쁜 코딩 일지 모르지만 학습 목적을 위해 쓰일 수 있으며, 처리기가있는 스레드를 이해하고 시스템에서 진행되는 작업과 이러한 오류를 해결하는 방법을 배우는 데 도움이 될 것입니다. –

+0

이 오류를 해결하면 목표에 훨씬 가까워 질 수 있을지 확신 할 수 없습니다. 솔직히, 나는 잘 작동하는 예제 코드로 시작하겠다. –

+0

나에게 계획을 듣는 것 같은 소리가 Chris에게 충고에 감사드립니다. –

0

당신이 스택 트레이스를 보면 당신은 답을 얻을 다음에서 onCreate 메소드 내 개체가 null의의.

06-26 21:48:55.387: E/AndroidRuntime(15578): Caused by: java.lang.NullPointerException 
06-26 21:48:55.387: E/AndroidRuntime(15578): at com.example.learninghandlers.MainActivity.onCreate(MainActivity.java:31) 

문제는이 라인 사이에 놓여 : 스택이 우리에게

setContentView(R.layout.activity_main); 
enter.setOnClickListener(this); 

때문에이 그렇게 하나가 null로 평가하는 것이 :

enter = (Button) findViewById(R.id.enter); 
start = (Button) findViewById(R.id.start); 
display = (TextView) findViewById(R.id.Display); 

그래서 문제는이에 확실히 , null이있는 것 :

R.id.enter 
R.id.start 

어떤 객체가 null인지 확인하기 위해 thru를 디버깅 할 수 있습니까? 이 기다릴 시도 할 때 호출 스레드가시의 모니터를 소유하고 있지 않기 때문에

+4

이것은 코멘트가 아닙니다. – Reimeus

+0

@Reimeus 주어진 질문에 대한 가능한 답변입니다. 확인할 수있는 모든 코드를 디버깅 할 수 없으므로 모든 대답에 대한 힌트가 있습니다. – Marco

+0

@Marco OP의 문제에 대한 직접적인 해결책을 제공하지 않기 때문에 아니다. –

관련 문제