3

Possible Duplicate:
Using global exception handling with “setUncaughtExceptionHandler” and “Toast”

I have implemented UncaughtExceptionHandler in onCreate() in one of my activities.

In uncaughtException() method I am trying to open another activity ErrorActivity with an extra parameter (error msg and stacktrace). That activity should only show (ment globaly) AlertDialog and handle logs etc.

Can some one tell me why the ErrorActivity doesnt open while the code in oncoughtException gets executed? I suspect the problem is Thread related.

Here is my first activity (simulating exception in onCreate())

public class MainActivity extends Activity {

    GlobalSettings settings;

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

        Thread.currentThread().setUncaughtExceptionHandler(
                new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread thread, Throwable ex) {
                Intent intent = new Intent(MainActivity.this,
                                               ErrorActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("ERROR", ex.getMessage());
                intent.putExtras(bundle);
                startActivity(intent); 
            } 
        } );

        settings = (GlobalSettings) getApplication();
        settings = null;
        settings.getApplicationContext();
        setContentView(R.layout.main);
    }
}

And my second activity that should handle errors:

public class ErrorActivity extends Activity {

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

        Bundle bundle = getIntent().getExtras();
        String name = bundle.getString("ERROR");
        ShowAlertDialog(name);
    }
}
Community
  • 1
  • 1
no9
  • 6,424
  • 25
  • 76
  • 115
  • have you got any error or force fully close the application please provide here logcat detail and your AndroidManifest file – Pratik Jul 01 '11 at 11:07
  • the AndroidManifest file is ok. Activity is registered there ok. No error happens. After startActivity() is called nothing happens (blank screen). LogCat is clean. – no9 Jul 01 '11 at 11:09
  • I suggest you to use FLAG_ACTIVITY_NEW_TASK and singleTask in Intent – ingsaurabh Jul 01 '11 at 11:14
  • Saurabh can you please provide example of the line? – no9 Jul 01 '11 at 11:18
  • ex. intent.setFlag(FLAG_ACTIVITY_NEW_TASK) put before start new Activity – Pratik Jul 01 '11 at 11:22
  • @Pratik i tried adding ( intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);) before starting activity and nothing changes. – no9 Jul 01 '11 at 11:42

2 Answers2

4

You can add attribute android:process=":report_process" to the <activity> element which refers to your bug report activity in AndroidManifest.xml.

By default, activities belong to the same appliction would run in the same process identified by your package name. By setting android:process attribute, you can override this. android:process starting with : refers to a private identifier within your package, so that you can start the activity in a new process without conflicting other packages' process.

peter
  • 1,034
  • 1
  • 9
  • 23
2

Refer Using global exception handling with “setUncaughtExceptionHandler” and “Toast”

There Qberticus told

You're not seeing anything because the exception happened on your UI thread and the stack unrolled all the way. So there is no more Looper and there is no support there that is used to display the Toast.

Since the exception happens on UI we cannot do an UI operation :(

Community
  • 1
  • 1
Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112