Thread.setDefaultUncaughtExceptionHandler` जब मेरी Android एप्लिकेशन एक अपवाद फेंक एक संवाद दिखाएं, मैं उपयोगकर्ता को बताने के लिए एक कस्टम संवाद दिखाना चाहते हैं वहाँ कुछ गलत हुआ है, इसलिए मैं Thread.setDefaultUncaughtExceptionHandler
का उपयोग एक वैश्विक अपवाद संचालक स्थापित करने के लिए:में `
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, final Throwable ex) {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("There is something wrong")
.setMessage("Application will exit:" + ex.toString())
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// throw it again
throw (RuntimeException) ex;
}
})
.show();
}
});
}
}
लेकिन मैं इसे किसी भी अपवाद नहीं है पाया, AlertDialog
नहीं दिखाया जाएगा, बजाय, आवेदन ब्लॉक और थोड़ी देर के बाद, यह सिस्टम संवाद दिखाएगा:
X app is not responding. Would you like to close it?
Wait | OK
अब मुझे क्या करना चाहिए ?
अद्यतन
लॉग:
11-16 12:54:16.017: WARN/WindowManager(90): Attempted to add window with non-application token WindowToken{b38bb6a8 token=null}. Aborting.
ऐसा लगता है त्रुटि से new AlertDialog.Builder(getApplicationContext());
आ रहा है लेकिन इस Application
उपवर्ग में अपवाद संचालक है, मैं कैसे निर्धारित कर सकते हैं इसके लिए एक गतिविधि उदाहरण?
लॉकेट को भी पेस्ट करें – waqaslam
यह संदेश तब दिखाई देता है जब एप्लाका टयन बहुत अधिक समय ले रहा है (उस मामले में कोई अपवाद नहीं है)। मेरी सिफारिश है कि अपने भारी कार्यों को एक AsyncTask में ले जाने का प्रयास करें। – richardtz