-1

抱歉,我发布了 Worng Logcat 信息时造成的混乱。我更新了问题。我想单击开始以启动一个线程,然后当单击输入时,我希望 thad 继续并获取消息并处理线程中的消息,然后将其输出到主线程并更新文本视图。我将如何启动一个线程以等待按 Enter 并获取处理程序的捆绑包?这是我的代码:

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

        }

    };

}
}

这是logcat信息:

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

2 回答 2

0

如果您查看堆栈跟踪,您会得到答案:onCreate 方法中的对象的 of 为空。

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

所以问题肯定出在这,有些东西是空的:

R.id.enter
R.id.start

你不能通过调试来检查哪个对象是空的吗?

于 2013-06-27T02:21:05.373 回答
0

您在此发布时当前的错误,

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

发生是因为调用线程在尝试 wait() 时不拥有其监视器。

要拥有监视器,您需要在此类的对象上同步的代码块内,或者在类的同步方法内。

但是,您对线程的整体使用看起来相当不规则,并且很可能隐藏在这个问题后面还有其他问题。

于 2013-06-27T04:25:16.620 回答