I am getting this error, and my application crashes:
java.lang.IllegalStateException: The current thread must have a looper!
I didn't get much about how to use looper on Google, I am using threads(mainly for sleep function), handler(for downloading the image while Async task is running) and Async task(for getting the JSON data from the URL). I have no idea how to resolve this issue, so any suggestions will be vey helpful.
This is the code for the thread which is executed on click of the button:
View view = flingContainer.getSelectedView();
view.findViewById(R.id.item_swipe_right_indicator).setAlpha((float) 1.0);
Thread timer = new Thread() {
public void run() {
try {
sleep(320);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
flingContainer.getTopCardListener().selectLeft();
}
}
};
timer.start();
I am using this libray and log-cat is:
where: at com.enormous.quotesgram.MainActivity$3.run(MainActivity.java:479) in last in log-cat corresponds to the line: flingContainer.getTopCardListener().selectLeft(); in above piece of code.
Try the following (unfortunately I cannot test the code):
Thread timer = new Thread() {
public void run() {
try {
sleep(320);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
runOnUiThread(new Runnable() {
#Override
public void run() {
flingContainer.getTopCardListener().selectLeft();
}
});
}
}
};
The idea behind is, that the Timer thread is not a Looper thread (resulting in an exception saying "The current thread must have a looper"). The UI thread however, is a Looper thread (see for instance this site).
As flingContainer.getTopCardListener().selectLeft() is probably designed to run on the UI thread it fails, if it not invoked in side of a pipelined thread.
Related
Consider this example:
Thread thread = new Thread(new Runnable() {
public void run() {
// Sleep for 5000ms
// Show toast message
}
});
Now I will start this thread on button click in MainActivity and right after that I would exit the activity on back button press, but in overided method onBackPressed following code is implemented:
If(thread != null)
thread.interupt();
finish();
After few seconds toast message is shown, why is that?
interrupt, clears the interrupt status of your thread and will cause the InterruptedException to be thrown. So if your thread is sleepin, and while it is asleep, you call interrupt, it will be woken up, and the execution flow will continue from the instruction that follows the catch block. Assuming you have something really simple like:
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackThreace();
}
runOnUiThread(TOAST);
}
or
public void run() {
while(true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackThreace();
}
runOnUiThread(TOAST);
}
}
both will shown the Toast even though you called interrupt()
While blackbelt has already explained the reason why this happens, here is how you can get around the problem.
The best way to interupt a thread is to use a if-boolean-break method.
So if i were to re-write you code it would be along the following lines
onBackPressed:
isBackPressed = true;
inside the thread's run method:
Thread thread = new Thread(new Runnable() {
public void run() {
// Sleep for 5000ms
if(!isBackPressed){
// Show toast message
}
}
});
I'm working on a service and I had the following code in onStartCommand:
try {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
Networker.get("http://google.com/");
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.run();
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Even though the networking code is running in another thread, my service still crashed with the NetworkOnMainThreadException. The equivalent AsyncTask code does not cause this error. Is it because of the join?
I was doing this because I only need my service to run a single request and then quit, so I wanted to use the code above and return START_NON_STICKY to make the service close quickly again. Am I supposed to be doing something else instead?
Call start() and not run() on the thread to start the thread.
run() will just run your Runnable in the current thread.
My function has to return data just after my thread end, I am using the wait() method after start() my thread but it doesn't work :
private class getDataThread extends Thread {
#Override
public void run() {
super.run();
while (true) {
try {
// ...
Thread.sleep(100);
} catch (InterruptedException e) {
// ...
}
}
}
}
public void getSensorValues(Bundle bundle) {
// ...
getDataThread gdt = new getDataThread();
gdt.start();
try {
gdt.wait();
} catch (InterruptedException e) {
// ...
}
}
in LogCat :
: An exception occurred during execution !
: Exception caught: java.lang.reflect.InvocationTargetException
: Exception cause: (SYSTEM) java.lang.IllegalMonitorStateException: object not locked by thread before wait() in getSensorValues
: status::FAILURE - output:: Possible errors: (SYSTEM) java.lang.IllegalMonitorStateException: object not locked by thread before wait() in getSensorValues.
What I'm doing wrong?
You're looking for join, not wait:
public void getSensorValues(Bundle bundle) {
// ...
getDataThread gdt = new getDataThread();
gdt.start();
try {
gdt.join();
} catch (InterruptedException e) {
// ...
}
}
wait has a different purpose, which is to signal another thread that an event has occurred. It requires a matching call to notify. Furthermore, you need to acquire the lock on the object which is being used to wait/notify, which is why you are getting that exception.
And another thing: starting a thread and then immediately joining it is redundant. You might as well execute everything on the main thread.
wait() does not wait for a thread to finish. It waits for another thread to call notify() or notifyAll().
Instead, you need to use join() so that the other thread will join the current thread. The current thread will block until the other thread finishes.
That said, both wait() and notify() need to be inside a synchronized block for the object being used. Example:
synchronized (lock) {
lock.wait();
}
I have an app that runs 2 threads in loops. 1st one is updating a graph in 1s interval and the second one is updating another graph at 60s interval. The second task is taking a long time since it is quering some server in the internet 3 times that might not always be available and even if it is it will take up to 5-7s to execute.
What is happening is when I launch the second thread it will pause execution of the first one and that is not what I want, I wish both run concurrently. Here in the Youtube video you can see the results of the app running. http://youtu.be/l7K5zSWzlxI
"thread_updater1s" is running a green graph, large readout, and a timer in the corner so you clearly see it stalls for 11 seconds.
1)First of all why is that happening? how to fix it?
2)I'm aware that I might not launch the threads properly at all. I had hard time understanding how to make something to run in a interval loop in Java and my code worked fine for one graph/tread. Now when I have 2 loops in separate threads I don't know why they are not executing concurrently.
Here is the code:
public class LoopExampleActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
thread_updater1s.start();
thread_updater2.start();
}// end of onCreate
final Runnable r1s = new Runnable() {
public void run() {
do_1s_updates(); // those are very quick http calls to the local API server
} // to get data nessessary for some plot.
// They have 1s timeout as well but rarely timeout
};
final Runnable r2 = new Runnable() {
public void run() {
do_large_updates(); //This makes 7 long call over the Internet to the slow https
//server once every 60s. Has 10s timeout and sometimes takes as much as
//7s to execute
}
};
Thread thread_updater1s = new Thread() {
#Override
public void run() {
try {
while (true) {
handler.post(r1s);
sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread thread_updater2 = new Thread() {
#Override
public void run() {
try {
while (true) {
handler2.post(r2);
sleep(60000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}
PS. please be forgiving and informative I only code Java for 15 days so far with absolutely no prior experince or lesson.
You need to make the http requests in the threads (not the posted runnables). Then, when you have the data downloaded, you create a runnable with that data that will update the graph and post that runnable to be executed by the UI thread. Here is an example:
public class LoopExampleActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
thread_updater1s.start();
thread_updater2.start();
}// end of onCreate
Thread thread_updater1s = new Thread() {
#Override
public void run() {
try {
while (true) {
final Object data = getDataFromServer1();
handler.post(new Runnable() {
#Override
public void run() {
updateGraph1(data);
}
);
sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread thread_updater2 = new Thread() {
#Override
public void run() {
try {
while (true) {
final Object data = getDataFromServer2();
handler.post(new Runnable() {
#Override
public void run() {
updateGraph2(data);
}
);
sleep(60000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Obviously, change that final Object data by the appropriate class that represents your data downloaded.
handler.post pushes the runnable onto the main (UI) thread's message queue for execution on the main thread.
So what you're doing is every sleep interval, you're sending a message to the main thread to run the function. Clearly, the main thread can't run 2 things at once, so that's why one runnable is delayed for the next one.
You probably want to do the work of the runnable in the separate threads - why did you start using a handler? What happens if you call do_1s_updates and do_large_updates directly instead of through the handler & runnable?
I was trying to run a small worker thread in the middle of an activity which would get some stuff from a web service and fill a container when it returned.
new Thread() {
#Override
public void run() {
Log.w("thread", "can read from thread");
try {
MyActivity.this.runOnUiThread(new Runnable() {
public void run() {
Log.w("internal thread", "running");
}
});
}
catch (Exception e) {
// etc
}
}
}.start();
The "internal thread: running" would never print in the log, and when I ripped out the contents and just pasted it into the main body of the method, it worked. My conclusion is that the doubly nested thread never ran. Why would that happen? Am I misusing runOnUiThread?
Thanks!
Why would that happen? Am I misusing runOnUiThread?
Yeas ! runOnUiThread is other thread than Main thread. i suggest to have a look at Android Handler.