I have implemented a thread in android which refresh the fragment (some text list ) for every 1 second .
its giving the runtime error while calling the fragment method at thread ,. here is my code
public class RunThreadExtended extends Activity implements Runnable
{
public void run() {
while(true)
{ try {
Thread.sleep(1000);
AndroidListFragmentActivity.strup++;
MyListFragment1 fragmentB = (MyListFragment1)getFragmentManager().findFragmentById(R.id.fragment1);
fragmentB.updatefrag();
} catch (InterruptedException e) {
e.printStackTrace();
}
}}}
If I call the fragment method from Mainactivity everything works from , since I need to refresh the thread for every 5 seconds in the backgroud I have implemented like this , but its not working ..pls suggest solution ...
You could also broadcast an Intent (context.sendBroadcast(intent)) in a thread and receive it in your Activity: Broadcast Receiver
I think you have to do the refreshing in
Or via a
Handler
, or via apost()
or in anAsyncTask
'sonProgress()
You are having errors because you are doing UI operations in a not UI thread. If you change the code into something like this, you will not have that error:
You can't update the UI on any thread except for the UI thread. If you want to update the thread you can use a handler that you send a message to every second (handlers handle all messages on the main UI thread).
See
Update UI from Thread
maybe you can do all the work inside a fragment. here are the steps.
below are the code snippet.