I have an Android Test Application in which I run several tests.
I use various assert calls such as assertEquals, assertTrue, assertNull and so on. When such an assert fails, I wish to save the stack trace to a file on the device. Does such a thing already exist or do you have any tips on how I could implement it?
You can just catch AssertionError
try {
assertEquals(true, true);
} catch (AssertionError ex) {
// code that write exception to file
}
Or more advanced you can use acra and implement your own ReportSender.
Related
Is there a way to block the displaying of thrown exception messages (with all the stacktrace and everything) for an android app ?
EDIT:
Well, I know I can use try/catch block to do that that's not what I want.
Is there like a way to specify that in mafinest file or maybe in project settigs or smthng ?
you can wrap your code in try catch
try {
// code that might throw an exception
} catch (Exception e) {
//don't print the exception
}
I am developing an Android-App with "Aide".Aide is an app for developing android apps with android devices. When i start the app, i have created, i get an error like "the app has aborted unfortunately". how can i resolve what happened wrong ? is there a log-file where i can see the stack trace ? is ist possible that everytime an error happens a dialog apperas with the stack trace instead of the message "the app has aborted" ? thanks for everybody who can help me.
Greets
Arne
If you want to observe the stack trace, all you need is a LogCat reader, like CatLog, for instance. Note that if your device is Jelly Bean of higher, you'll need root permissions to read the logs.
EDIT:
Further research indicates that there is a LogCat reader built into AIDE. The root permission issue still applies.
I have never used Aide, but the concept will be the same. You need to be able to debug your app on your phone via your IDE. As an example in Eclipse I would connect my phone via usb and in Eclipse it then shows up as an Android Device in AVD. I then run my App in Debug mode on my phone and all your error output will be in Logcat. Otherwise you will have to code debug logic into your app so that it writes it's own logging onto your fs on you phone.
If you have the Android SDK installed (I guess it's the case), then you can use the adb utility to access the log :
adb logcat
This will show you stacktrace in case of error, and many very other useful informations.
You got 3 options:
Upgrade into a stable Pro version to use the working LogCat on AIDE
Use USB debugging as mentioned by apesa
Use following function to log to local file:
public void appendLog(String text)
// https://stackoverflow.com/a/6209739/8800831
{
File logFile = new File("sdcard/log.file");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf. flush();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Use it like this:
try{
// your code goes here
}catch (Exception e){
appendLog(e);
}
You need to add permission for writing_external_storage in Manifest.
02-07 10:49:45.558: E/dalvikvm-gc(7184): Could not create 2617344-byte ashmem mark stack: Too many open files
While I was playing the game and after some time the game forced closed showing the above error in Log Cat. Is there any solution for this?? and what could be its cause??
The cause is that your code is not closing files and the system is running out of file handles. You should always use files using a coding pattern that ensures that all streams are closed when no longer needed. Here's a typical pattern:
InputStream is = null;
try {
is = new FileInputStream(myFile);
// read stuff from is
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
Log.w(LOG_TAG, "Exception raised when closing file", e);
}
}
}
The outer try could also have a catch clause. The reason for the try/catch in the finally clause is to prevent an exception in closing the file from suppressing any exception raised in the outer try block.
Writing to files tends to be less of a problem because programmers usually take care to close the file to ensure that the data reaches the file. However, a similar pattern should be followed there: never let an exception prevent the closing of a file.
In my app I want to catch all types of exceptions and send reports by e-mail. For that I'm using global try catch block. But now I need to recognize exception by type. How can I do it?
try{
...
}
catch (Exception e){
//Here I need to recognize exception by type
send(Error);
}
Why you don't simple send the whole stacktrace?
send(e.getStackTrace())
It not only contains the Exception type but also where (file, class, line) it occurred.
Additionally, you can also simply use the toString() method.
See the java doc for further information
Instead of rolling your own error logging and reporting mechninism I strongly recommend you use ACRA Its free, open source, and supports sending error logs to email. I have used it for quite some time and it is very good.
This will give you all sorts of information such as phone make, model, resolution, free memory, as well as a full stack trace of the error. Its by far the easiest way to get quality error reporting into an Android app.
The best part is it takes all of about 5 minutes to get setup and integrated.
e.getClass() // will give you Class object
e.getClass().getName() // will give you class name
However if you know the class names already you can use
if(e instanceof A)
{
// some processing
}
else if(e instanceof B)
{
//some processing
}
else
{
//
}
Is it possible to get variable values included in a stack trace? I have just started using bugsense which emails the stacktrace to me and I wonder if there is some way in my code to put the variable values into the stacktrace output
Not by default, you have to do it by yourself:
The stack trace will only tell you about the involved line of code (where the Exception is thrown) and the execution stack.
But nothing prevents you from catching the Exception and to include some debug information in the message:
try {
...the code...
}
catch (Throwable t) {
// Here, we catch any Throwable (Exception but also Error such as OutOfMemory
// or NoClassDefFound), which is *absolutely not suitable* for
// anything else than debugging.
// You can (should, actually) make this catch statement more specific
// depending of the Exception or Error you are facing
// Dump your variables here:
final String message = "myVar=" + myVar;
// The statement below rethrows the original Throwable and adds your
// own message to it
throw new RuntimeException(message, t);
}
Or to put a breakpoint in the catch { } statement to inspect the state of your application at that stage, but as I understood it may not be applicable in the case you are describing.
(By the way, I suggest you add the tag "Java" to your question. This way it will also be visible by the Java community of StackOverflow)