-1

Sorry for the confusion I posted the Worng Logcat info. I updated the question. I want to click Start to start a thread then when enter is clicked i want the thad to continue and get the message and handle the message in the thread then output it to the main thread and update the text view. How would i start a thread to wait for enter to be pressed and get the bundle for the Handler? Here is my Code:

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

        }

    };

}
}

Here is the logcat info:

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)
Dakota Miller
  • 493
  • 1
  • 4
  • 22
  • It would be better if you post the stacktrace of the app instead of the logcat. – Luiggi Mendoza Jun 27 '13 at 02:18
  • It looks like it's coming in your `onCreate()` method. With line numbers, I can be more specific. Chances are you either have your ids wrong, or you aren't inflating your layout before using it. – David Merriman Jun 27 '13 at 02:21
  • 1
    Which is the line 31? – Luiggi Mendoza Jun 27 '13 at 02:22
  • 3
    There is *exactly* one reason for an NPE; you have a reference variable whose value is null, and you try to use it. Whatever is on line 31 is null. – Brian Roach Jun 27 '13 at 02:45
  • new myThread().hello.start(); is where the problem is at when the thread is called the app throws the error. – Dakota Miller Jun 27 '13 at 02:50
  • How would I fix this though. – Dakota Miller Jun 27 '13 at 02:51
  • As BrianRoach and me have posted in comments, the problem is in line 31 of this class, but you haven't shown which one is. Your problem is unlikely to be solved without this info. – Luiggi Mendoza Jun 27 '13 at 02:58
  • ok im looking it up now. – Dakota Miller Jun 27 '13 at 03:06
  • mHandler = new Handler() { This is Line 31. – Dakota Miller Jun 27 '13 at 03:11
  • @DakotaMiller - that seems unlikely, are you absolutely sure that is line 31? I'd think that if the null pointer exception were occurring in the inner class it would tell you that in the stack trace. – Chris Stratton Jun 27 '13 at 03:32
  • Line 31 could not possibly cause a null pointer exception. – FabianCook Jun 27 '13 at 03:36
  • when i look at eclipes (and added the line numbers in) thats what it shows as line 31 – Dakota Miller Jun 27 '13 at 03:37
  • @SmartLemon - the stack trace doesn't lie, so the likely case is that line 31 has been incorrectly identified in the post. Perhaps a different version of the code was examined than was installed at the time the stack trace was produced. – Chris Stratton Jun 27 '13 at 03:38
  • lke i said before i belive the error is in new myThread().hello.start(); when i move this to on create the app doesnt work at all. but when its in the case for the start button the app workes untill start is pressed. – Dakota Miller Jun 27 '13 at 03:40
  • Chris, read his comments, he is saying that is it from `new myThread().hello.start();` or the onclick event of `R.id.start` – FabianCook Jun 27 '13 at 03:40
  • That is not possible **for the version of the code which has been posted** as that code is not contained in the onCreate() method which was **explicitly identified by the stack trace as containing the culprit**. If the version tested had that added to the onCreate() method, then it's that version which needs to have its line 31 identified. – Chris Stratton Jun 27 '13 at 03:44
  • Well maybe you should just take it that he miss posted the code and miss identified the line number. – FabianCook Jun 27 '13 at 03:45
  • yes sorry that was my fault i had posted the worng logcat and im updating it now again im sorry. – Dakota Miller Jun 27 '13 at 04:03
  • Could you please also identify the correct line that is causing the error. – FabianCook Jun 27 '13 at 04:07
  • This is an entirely different and unrelated error you have now. Your thread usage seems more than a bit improper - do you really want to have a thread object which immediately creates another yet another thread? And what are you actually trying to accomplish? There is almost certainly a better structure for building functionality. – Chris Stratton Jun 27 '13 at 04:11
  • @SmartLemon I updated the question with the right logcat Note im asking a different question. – Dakota Miller Jun 27 '13 at 04:11
  • @ Chris Stratton ,Yes like I im sorry for the error that was my fault. My goal is to get an input from an edittext at some point and handle it to a thread weather it being were its at or in a different class andthe do some work and update the UI from there. – Dakota Miller Jun 27 '13 at 04:15
  • You could easily do that with an AsyncTask for the work and runOnUiThread() for updating the UI with the output. – Chris Stratton Jun 27 '13 at 04:17
  • Yes I could. I'm using this app solely for learning purposes for Handlers I've been able to get it to work to update the ui through the thread without the thrad handler but when i added the thread handler im not sure how to implement the input to from an edittext when the enter button is pressed. – Dakota Miller Jun 27 '13 at 04:22

2 Answers2

0

If you look at the stack trace you get the answer: of of your object within the onCreate method is 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)

The problem lies between these lines:

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

because the stack tells us that, so one of these evaluate to null:

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

So the problem is definitely in this, something there is null:

R.id.enter
R.id.start

Can you not debug thru, to check which object is null?

MeTitus
  • 3,390
  • 2
  • 25
  • 49
  • 4
    This is a comment not an answer – Reimeus Jun 27 '13 at 02:29
  • @Reimeus it is the possible answer to the given question. You cannot debug of have all the code available to check, so all your answer can have is hints. – MeTitus Jun 27 '13 at 02:30
  • @Marco it is not since it doesn't provide a direct solution to OP's problem – Luiggi Mendoza Jun 27 '13 at 02:32
  • @LuiggiMendoza feel free to give a more concrete answer to the OP, having that code and stack which only shows that an exception is being throw in a method context. – MeTitus Jun 27 '13 at 02:34
  • i've messed around with it a little bit here and the problem lies here in this line new myThread().hello.start(); when i change this and put this in the oncreate() the app wont run at all. but i want it to be called when the button start is clicked. so the problem is when i call the thread to start. – Dakota Miller Jun 27 '13 at 02:45
  • 1
    If you had developed a basic android application before, you will understand that nor `R.id.enter` nor `R.id.start` is `null` since they would be `int` variables. Also, I did what I could to answer this that is asking which line is the number 31 (as you can see in OP's question). After that, the code looks fine and shouldn't have any exception. – Luiggi Mendoza Jun 27 '13 at 02:47
  • @LuiggiMendoza I did do any major Android app, but I've been working with java since 2004 as such 'R' does not seem int to me, but then again you seem to know it all. I'm done with you. – MeTitus Jun 27 '13 at 02:51
  • @DakotaMiller but if that was the case you would not be getting this: 06-26 21:48:55.387: E/AndroidRuntime(15578): at com.example.learninghandlers.MainActivity.onCreate(MainActivity.java:31), before the null exception. – MeTitus Jun 27 '13 at 02:54
  • Well, looks like you don't otherwise you would know that `R` is an autogenerated class handled by the Android application itself (you don't need to create it nor edit it). I don't know it all but this doesn't add any additional info besides the one posted in comments on OP's question. – Luiggi Mendoza Jun 27 '13 at 02:59
  • ok, how would i go about fixing this error? – Dakota Miller Jun 27 '13 at 03:01
  • @DakotaMiller You have to set a breakpoint in the onCreate method and check which object is null, there no other way. – MeTitus Jun 27 '13 at 03:04
  • @Marco - incorrect, there are plenty of other ways, such as logging suspects. An interactive debugger is available on Android, but is **not required** to solve this problem. – Chris Stratton Jun 27 '13 at 03:24
  • @Chris Stratton Then how can i solve this problem? – Dakota Miller Jun 27 '13 at 03:30
  • By accurately identifying line 31 **in the exact same version of the code which caused this stack trace** (or whatever trace you get presently) Or if that is too hard, by logging every object utilized in the onCreate() method (there aren't many of them) so you can see which one is null. Or by running the debugger if you like - the point wasn't that the debugger won't work, but that it isn't the only way as was claimed. – Chris Stratton Jun 27 '13 at 03:53
0

Your current error at the time of this posting,

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

occurs because the calling thread does not own its monitor at the time when it attempts to wait().

To own the monitor, you need to be inside of a block of code synchronized on your object of this class, or inside a synchronized method of your class.

However, your overall use of threads looks rather irregular, and it is likely that there are additional problems hiding behind this one.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • Thank you Chris I know this may rather bad coding but its for learning purposes and will help me learn how to understand threads with handlers and whats going on in the system and how to solve these Errors. – Dakota Miller Jun 27 '13 at 04:37
  • I'm not sure that solving this error is going to put you that much closer to your goal. Honestly, I'd start over with known-working example code. – Chris Stratton Jun 27 '13 at 04:39
  • Sounds like a plan to me thanks Chris for the advice. – Dakota Miller Jun 28 '13 at 04:44