Tag Archives: java

Display a stack trace in the android debugger

I’ve found that when developing an application for android, it is not obvious how to display a stack trace in the debugger in order to assist with finding the cause of a problem. If you don’t catch the exception, the stack trace will display OK. However often you want to catch the exception, but when debugging you want to find out what caused it by logging it to the debugger.

In normal java, you might use e.printStackTrace, but this won’t work without the use of log redirecting in the adb as it is normally sent to /dev/null

I’ve written a small utility class to do this:

[code lang=”java”]
package com.example;

import android.util.Log;

* A class to display a traditional Java stack trace in the android debugger
* @author Stuart Lewis (stuart@stuartlewis.com)
public class StackTraceLogger {

* Display a stack trace
* @param e The Exception
* @param tag The tag to use in the debugger
public static void getStackTraceString(Exception e, String tag) {
Log.e(tag, e.toString());
for (StackTraceElement ste : e.getStackTrace()) {
Log.e(tag, ste.toString());

To make use of this, in your catch use:

StackTraceLogger.getStackTraceString(e, TAG);

(where ‘e’ is the Exception, and TAG is your logging TAG). I hope this is useful.mobile java games

Java, RTF, and unicode characters

I don’t often post code snippets on my blog, however I wasn’t able to find any advice recently concerning how to put Unicode / UTF-8 characters into an RTF (Rich Text Format) document that was being created in Java.

Here’s the problem:

I have some data that I am manipulating Java, and the data contains some unicode characters.  I need to include them in an RTF document, however RTF documents only support 7-bit ASCII.

The Unicode characters need encoding in some way. Wikipedia (http://en.wikipedia.org/wiki/Rich_Text_Format#Character_encoding) says the solution is to use the ‘/u[character-code]’ notation.  The issue I had was how to get from unicode characters to that solution.  The answer is in the form of the codePointAt method in Java, which will return the character code for the character referenced.  This replacement can then be fed through the replaceAll method in a String.

Here’s the solution:

// Create a hashtable to hold the characters to convert
Hashtable<String, String> replace = new Hashtable<String, String>();

// The String to convert
String s = "The Māori Macron";

// Values we’ll use in the loop
int value;
String bit;
for (int i = 0; i < x.length(); i++) {
// Get the character value
bit = x.substring(i, i + 1);
value = x.codePointAt(i);

// If the character value is above the
// 7-bit range of RTF ASCII
if (value > 127) {
replace.put(bit, "\\\\u" + value + "\\\\’ ");

// Now replace all the characters we found
Enumeration e = parameters.keys();
String key, value;
while (e.hasMoreElements()) {
// Get the key
key = (String)e.nextElement();

// Get the value
value = (String)parameters.get(key);

// Make the substitution
s = s.replaceAll(key, value);

In the example above, the String ‘The Māori Macron’ is converted into ‘The M\u257\’ ori Macron’ which is valid RTF to show the characters you need.  I’m using a separate enumeration of the strings to be replaced, as my scripts also have to perform other substitutions such as converting <b> to bold text etc.продвижение сайта по звонкам