views:

74

answers:

4

I have thread in "onCreate" which is getting content from web. While the content is getting, I have progress dialog.

new Thread() {

     public void run() {

     Get_content() ;


    handler.sendEmptyMessage(0);
     }

     }.start();

But if I rotate the display (to landscape mode) while this is running, my application gets Force close . In the log I have this:"thread main exiting due to uncaught exception"

A: 

Did you try just adding a try/catch block in main and logging any exception that's getting thrown?

Curtis
A: 

Here is my code:

 public class Example extends Activity {

  TextView status;

  private ProgressDialog progressDialog;

  private Handler handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
  super.handleMessage(msg);
  progressDialog.dismiss();
       }

  };
     @Override
     public void onCreate(Bundle savedInstanceState) {
    try{    super.onCreate(savedInstanceState);
        setContentView(R.layout.lista);
        progressDialog = ProgressDialog.show(Example.this, "", "Loading data...");
       new Thread() {
        public void run() {
        Get_content() ;
       handler.sendEmptyMessage(0);
        }
        }.start();
 status=(TextView)findViewById(R.id.TextStatus);

  } catch (Exception e){} }

 public void Get_content() {
         try {
          String address;
          address="http://example.com";
           URL myURL = new URL(address);
          URLConnection ucon = myURL.openConnection();
           InputStream is = ucon.getInputStream();
           BufferedInputStream bis = new BufferedInputStream(is);
           ByteArrayBuffer baf = new ByteArrayBuffer(50);
           int current = 0;
           while((current = bis.read()) != -1){
                   baf.append((byte)current);
           }
         myString = new String(baf.toByteArray());
         } catch (Exception e) {}
       status.setText(myString);
     }
     }
Can you post the logcat please? Need to see what the actual error is. But if I had to guess the problem, it's probably failing on the progressDialog...
xil3
How to rotate the emulator while the program is running ? Please tell me if it is possible , because I have debugger on my phone and I need manual to type log.
+1  A: 

What is happening is on orientation change, the activity is killed and re-created. But since you started a thread and the thread is still running, it is not killed, and when it finishes and tries to close the Progress Dialog, that causes the FC because the progress dialog doesn't exist. This is a pretty common problem, you can check out the droid-fu library that tries to fix this, or you can be smarter with your Threaded task (like if it is going to take a decent amount of time to complete, then maybe put it in a background service).

BrennaSoft
Is it smartly to put code which closes the dialog in try catch block ? I think then I won't get FC.
A: 

@xil3 here is my log:

W/dalvikvm(17144): threadid=3: thread exiting with uncaught exception (group=0x40013140) E/AndroidRuntime(17144): Uncaught handler: thread main exiting due to uncaught exception E/AndroidRuntime(17144): java.lang.IllegalArgumentException: View not attached to window manager E/AndroidRuntime(17144): at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:356) E/AndroidRuntime(17144): at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:201) E/AndroidRuntime(17144): at android.view.Window$LocalWindowManager.removeView(Window.java:400) E/AndroidRuntime(17144): at android.app.Dialog.dismissDialog(Dialog.java:268) E/AndroidRuntime(17144): at android.app.Dialog.access$000(Dialog.java:69) E/AndroidRuntime(17144): at android.app.Dialog$1.run(Dialog.java:103) E/AndroidRuntime(17144): at android.app.Dialog.dismiss(Dialog.java:252) E/AndroidRuntime(17144): at com.webservice.KursnaLista$1.handleMessage(KursnaLista.java:77) E/AndroidRuntime(17144): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime(17144): at android.os.Looper.loop(Looper.java:123) E/AndroidRuntime(17144): at android.app.ActivityThread.main(ActivityThread.java:3948) E/AndroidRuntime(17144): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(17144): at java.lang.reflect.Method.invoke(Method.java:521) E/AndroidRuntime(17144): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) E/AndroidRuntime(17144): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) E/AndroidRuntime(17144): at dalvik.system.NativeStart.main(Native Method) I/dalvikvm(17144): threadid=7: reacting to signal 3 I/ActivityManager( 59): Process com.webservice (pid 17144) has died.