1

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 ...

indra
  • 832
  • 4
  • 17
  • 33

5 Answers5

5

I think you have to do the refreshing in

yourActivity.runOnUiThread(new Runnable() {
public void run()   { /* here */ });

Or via a Handler, or via a post() or in an AsyncTask's onProgress()

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
1

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:

public class RunThreadExtended extends Activity implements Runnable
{
   public void run() {

   while(true)
   {  try {
       Thread.sleep(1000);
       AndroidListFragmentActivity.strup++;
       RunThreadExtended.this.runOnUiThread(new Runnable() { //Use the runOnUIThread method to do your UI hanlding in the UI Thread
           public void run()   {
               MyListFragment1 fragmentB = (MyListFragment1)getFragmentManager().findFragmentById(R.id.fragment1);
               fragmentB.updatefrag();
           }
       });
   } catch (InterruptedException e) {
        e.printStackTrace();
   }
   }}}
AggelosK
  • 4,313
  • 2
  • 32
  • 37
0

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

Community
  • 1
  • 1
Kaediil
  • 5,465
  • 2
  • 21
  • 20
  • There are several ways to update the UI thread, handler like you said also post for example or even change to a asynctask. – basickarl Oct 31 '13 at 04:14
0

You could also broadcast an Intent (context.sendBroadcast(intent)) in a thread and receive it in your Activity: Broadcast Receiver

benkdev
  • 673
  • 2
  • 16
  • 32
0

maybe you can do all the work inside a fragment. here are the steps.

  1. define the Handler and Runnable in the fragment.
  2. create the Handler and Runnable in the onCreate() and onAttach()
  3. post the Runnable job in the Handler in onStart().
  4. remove the Runnable job out of the Handler in onStop().

below are the code snippet.

public class JobDetailFragment {

private Handler m_Handler;
private Runnable m_Runnable;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    m_Runnable = new Runnable() {

        @Override
        public void run() {

            updateJobStatus();
        }
    };
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    m_Handler = new Handler();
}

@Override
public void onStart() {
    super.onStart();

    if (m_Handler != null) {
        m_Handler.postDelayed(m_Runnable, PrinterOnUIConstants.PRINT_JOB_AUTO_UPDATE_INTERVAL);
    };
}

@Override
public void onStop() {
    super.onStop();

    // cancel the potential enqueued callback.
    if (m_Handler != null) {
        m_Handler.removeCallbacks(m_Runnable);
    }
}
Zephyr
  • 6,123
  • 34
  • 33