I have a class (in it's own file)
public class UncaughtExceptionHandler implements
java.lang.Thread.UncaughtExceptionHandler
In my main program I have the following:-
Thread.setDefaultUncaughtExceptionHandler( new UncaughtExceptionHandler());
When compiling I get the following error:-
cannot instantiate the type Thread.UncaughtExceptionHandler
Am I missing some basic point here?
Here is the code: import java.io.*;
import android.content.*;
public class UncaughtExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler { private final Context myContext;
public UncaughtExceptionHandler(Context context) {
myContext = context;
}
public void uncaughtException(Thr开发者_如何学Goead thread, Throwable exception) {
boolean crashedOnLastClose = false;
MyApplication.writeProgressLog("before isfinishing".ERROR_FILE_NAME);
// The application is being destroyed by the O/S
crashedOnLastClose = true;
MyApplication.writeProgressLog("after isfinishing",ERROR_FILE_NAME);
}
}
ISSUE
In the statement
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler());
new UncaughtExceptionHandler()
is actually
new java.lang.Thread.UncaughtExceptionHandler()
This is throwing the exception
SOLUTION
Check file UncaughtExceptionHandler.java
Find the package: (on the top of the file)
package your.package;
Now go to your main program
Replace
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler());
with
Thread.setDefaultUncaughtExceptionHandler(new your.package.UncaughtExceptionHandler());
UncaughtExceptionHandler is an interface, you cannot instantiate an interface. You need to implement its methods.
Your code should look like this:
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// your code
}
});
Hope this helps.
My IDE is having a similar problem with the same construct using @Override. The method uncaughtException(Thread, Throwable) of type new Thread.UncaughtExceptionHandler(){} must override a superclass method:
Thread.UncaughtExceptionHandler uce = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
String s = "ssh to user@" + host + " failed";
log.error(s);
throw new IllegalStateException(s, e);
}
};
精彩评论