MapView and AsyncTask - android

I am trying to use an AsyncTask class that I created to update a MapView. The problem is I am getting this error when I make my call to execute the AsyncTask:
"Can't create a Handler inside a thread that has not called Looper.prepare()"
I have tried running the Task on the UI Thread using
Handler hand = new Handler(Looper.getMainLooper());
hand.post(new Runnable() {
public void run() {
new RxThread().execute();
}
});
But that just gives me the same error. I realize that my MapView doesn't call looper prepare, and that I'm having troubles getting this to work since I'm running the MapView on a seperate activity rather than the Main Activity. Does anyone have a good solution to this?

IIRC, your error is because you are first referencing AsyncTask on a background thread. You can only create and execute() an AsyncTask on the main application thread.

Related

Why Android myActivity.runOnUiThread and uihandler.post hangs my UI?

I have a method invoked by onClickListener
#Override
public Object getData() {
Thread t = new Thread(new testThread());
t.start();
return false;
}
it start the new Thread well, but when I am trying to do both:
private class testThread implements Runnable{
#Override
public void run() {
OuterClass.this.myActivity.runOnUiThread(new Runnable(){ ... });
OuterClass.this.myActivity.uiHandler.post(new Runnable(){ ... });
...
nothing happens. UI hang up and no Runnable never run (I see it during careful debugging).
Why? Everything should work or even if it fail, why the UI hangs??
Please help!
SOLVED!!! The problem was in method which invokes getData (outside the scope), it never finished failing into infinite loop. Since that scheduled Runnables never started as I think. Now everything works .
AsyncTask is better. It is easier to use, you don't have to manually manage so many threads, and it keeps your code clean.
The doInBackground() method will do your lengthy task on an alternate thread. If you want to update your UI when the task is running, use the publishProgress() method, and if you want to update the UI after the work is done, use onPostExecute().
As to your question on why the UI hangs, see if you are using any method that takes very long or blocks for some reason in the runOnUiThread() method. If any code takes time, remove it from this method.

Android background task

I need to start background task. Task call async http request and if condition is valid show alert.
I implemented it but I receive this error:
java.lang.RuntimeException:
Can't create handler inside thread that has not called
Looper.prepare()
This is my java code:
Timer timer = new Timer();
timer.schedule(new RequestScheduledTask(), intervalTask);
class RequestScheduledTask extends TimerTask {
#Override
public void run() {
myCall();
}
}
Can you help me please?
Thanks
Luca
for example I need refresh adapter list when data is changed. Data returned from webserver could to be changed or not. if it is changed I must refresh listview/adapter, else nothing.
myCall call server, get data response (using other framwork like volley) and then update list view.
When myCall start I want display loading indicator but when I show it, app crash with runtime exception
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
According to the following post you are doing something in a thread that is meant to be called on the main thread! What is myCall(); doing?
Can't create handler inside thread that has not called Looper.prepare()
And don't do http request with async tasks thats bad practice.
I would recomment to use volley:
http://www.itsalif.info/content/android-volley-tutorial-http-get-post-put
[edit]
Okay its the loading indicator that is should be called on the main thread. I guess you are doing it in the AsyncTask.
You can use Activity.runOnUiThread(Runnable()) to run a runnable to display it.

Handler Does Not Execute Run

This feels like it should be a very simple task but I am having a lot of problems with it. In my program I have extended the WebView class for my own and am trying to add it to a layout programatically. Here is my code:
Looper.prepare();
Handler handler = new Handler();
handler.post(new Runnable() {
public void run() {
MyWebView webView = new MyWebView(context,1,2,3,4);
appState.projectWebView.add(webView);
addView(webView);
}
});
When I run this code it does not execute. I have no idea why. Thanks for you time.
You cannot create a Handler outside of an UI thread. Well, you can, but you will have to turn that thread into a message queue with much more than just Looper.prepare().
What you need to do is pass an Activity to the class that contains the code in your sample, and call runOnUiThread() on it. Alternatively, you can pass a Handler created on a UI thread, for instance create it at the thread that runs your Activity UI, and then call post on that handler.
Note that this is awful advice, you seem to be trying to do things against the Android framework. But, without further information of what you are actually trying to do, there is no much that can be said.

A WebView in a thread can't be created

i have some threads in which i create some views and prepare them to be displayed. Among them i also have a WebView. This code is executed in thread:
WebView lGraphWebView = null;
try{
lGraphWebView = new WebView(AppController.getAppController());
}catch (Exception e) {
Log.d("info", "error: " +e );
}
and it throws the following exception:
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
This is a bit strange, because when i create a simple button all is OK. So, can anyone explane to me why on creation of a WebView i get this exception and if Looper.prepare() can help here?
Thanks in advance!
In general, its not safe to create view outside of main thread.
In your particular case, this is not allowed, because WebView creates Handler() in its constructor for communication with UI thread. But since Handler's default constructor attaches itself to current thread, and current thread does not have Looper running, you're getting this exception.
You might think that creating a looper thread (that must be alive at least as long as WebView) might help you, but this actually a risky way to go. And I wouldn't recommend it.
You should stick with creating WebViews in main thread. All controls are usually optimized for fast construction, as they are almost always created in UI thread.
You should not create or manipulate views in threads other than the main UI thread. For instance, you can use the Handler to post to the UI thread:
private Handler handler = new Handler();
handler.post(new Runnable() {
public void run() {
lGraphWebView = new WebView(AppController.getAppController());
}
});

Exception while Starting an Activity from TimerTask

I'm trying to start a an activity only after some data is ready in the Service
I'm trying this with a timer task which constantly polls the service for the data readness
public class SplashTask extends TimerTask {
#Override
public void run() {
Log.i(MY_DEBUG_TAG, "Internet is accessible, Running some Spalsh screen Tasks ");
if(mBoundService.isDataReady()) {
Log.e(MY_DEBUG_TAG, "Data is ready in service..");
startActivityForResult(new Intent(SplashDroid.this, FunWithDataActivity.class), 3);
} else {
Log.e(MY_DEBUG_TAG, "Data not ready in service..");
}
Log.i(MY_DEBUG_TAG, "Spalsh Tasks fnished..");
}
}
Issue is that when data is ready and FunWithDataActivity about to start, i'm getting the following error
07-27 14:53:40.614: ERROR/AndroidRuntime(1042): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
startActivityForResult has to be called from the UI thread (which is not the thread in which the handler executes). To achieve this, move the startActivityForResult code to a Runnable and run it using runOnUiThread inside the run().
You can't use startActivityForResult from non-UI thread. You can either use runOnUiThread() or Handler.post().
Also, you shouldn't really use separate thread for polling. Use Handler's postDelayed() function for polling. This way you won't wasted whole thread for simple polling. For an example see: Repeat a task with a time delay?
Try to use the CountDownTimer class instead. You can also see this answer for an example: TimerTask in Android?
Worth looking into these post:
Can't create handler inside thread that has not called Looper.prepare()
Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog
If not resolved,Could u post your code where u r facing the problem!!

Categories

Resources