Java Exceptions in AndroidRuntime Error Log

This section provides a tutorial on how use the 'adb logcat' command to retrieve Java exceptions from the AndroidRuntime error log entries.

In order to see what will happen if an Android application runs into a problem, I wrote another new application called "AndroidDebug" with this Java code:

/* AndroidDebug.java
 * Version 1.0 - Set activity content to a child view
 * Copyright (c) 2015, HerongYang.com, All Rights Reserved.
 */
package com.herongyang.debug;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.Button;
public class AndroidDebug extends Activity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      // Create the layout
      LinearLayout l = new LinearLayout(this);

      // Create the left button
      Button bl = new Button(this);
      bl.setText("Yes");
      l.addView(bl);
      
      // Create the right button
      Button br = new Button(this);
      br.setText("No");
      l.addView(br);

      // Set the left button as the activity content      
      setContentView(bl);
   }
}

Build the new application project and install it on the emulator.

Go to the emulator and try to launch "AndroidDebug". I see this error message on the screen: "Unfortunately, AndroidDebug has stopped".

Android View Problem
Android View Problem

The application crashed. But don't worry. We can use the "adb logcat" command to find out more information about the crash.

C:\herong\AndroidDebug>\local\android-sdk-windows\platform-tools\adb \
   logcat -b crash AndroidRuntime:E *:S

...
E/AndroidRuntime(  812): FATAL EXCEPTION: main
E/AndroidRuntime(  812): java.lang.RuntimeException: Unable to start \
   activity ComponentInfo{com.herongyang.debug \
   /com.herongyang.debug.AndroidDebug}: \
   java.lang.IllegalStateException: The specified child already has \
   a parent. You must call removeView() on the child's parent first.
E/AndroidRuntime(  812):    at android.app.ActivityThread.performL...
E/AndroidRuntime(  812):    at android.app.ActivityThread.handleLa...
E/AndroidRuntime(  812):    at android.app.ActivityThread.access$6...
E/AndroidRuntime(  812):    at android.app.ActivityThread$H.handle...
E/AndroidRuntime(  812):    at android.os.Handler.dispatchMessage(...
E/AndroidRuntime(  812):    at android.os.Looper.loop(Looper.java:...
E/AndroidRuntime(  812):    at android.app.ActivityThread.main(Act...
E/AndroidRuntime(  812):    at java.lang.reflect.Method.invokeNati...
E/AndroidRuntime(  812):    at java.lang.reflect.Method.invoke(Met...
E/AndroidRuntime(  812):    at com.android.internal.os.ZygoteInit$...
E/AndroidRuntime(  812):    at com.android.internal.os.ZygoteInit....
E/AndroidRuntime(  812):    at dalvik.system.NativeStart.main(Nati...
E/AndroidRuntime(  812): Caused by: java.lang.IllegalStateException: \
   The specified child already has a parent. You must call \
   removeView() on the child's parent first.
E/AndroidRuntime(  812):    at android.view.ViewGroup.addViewInner...
E/AndroidRuntime(  812):    at android.view.ViewGroup.addView(View...
E/AndroidRuntime(  812):    at android.view.ViewGroup.addView(View...
E/AndroidRuntime(  812):    at com.android.internal.policy.impl.Ph...
E/AndroidRuntime(  812):    at com.android.internal.policy.impl.Ph...
E/AndroidRuntime(  812):    at android.app.Activity.setContentView \
   (Activity.java:1855)                                           ...
E/AndroidRuntime(  812):    at com.herongyang.debug.AndroidDebug.\
   onCreate(AndroidDebug.java:30)                                 ...
E/AndroidRuntime(  812):    at android.app.Activity.performCreate(...
E/AndroidRuntime(  812):    at android.app.Instrumentation.callAct...
E/AndroidRuntime(  812):    at android.app.ActivityThread.performL...
E/AndroidRuntime(  812):    ... 11 more
...

Do you see what is the cause of the problem? The statement, "setContentView(bl);" is trying to set View object "bl" as the Activity content view. But it already have a parent view, the layout "l".

Fixing the problem is easy. I will leave it you.

By the way, on Android 4.0.3, the AndroidRuntime log is in the "main" buffer. You need to run the following command to see the RuntimeException:

C:\herong\AndroidDebug>\local\android-sdk-windows\platform-tools\adb \
   logcat -b main AndroidRuntime:E *:S

Last update: 2015.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows System

 Installation of Android SDK R24 and Emulator

 Installing Apache Ant 1.9 on Windows System

 Developing First Android Application - HelloAndroid

 Android Application Package (APK) Files

 Android Debug Bridge (adb) Tool

 Android File Systems

 Android 4.0.3 File Systems

 AboutAndroid - Application to Retrieve System Information

 android.app.Activity Class and Activity Lifecycle

 View Objects and Layout Resource Files

Using "adb logcat" Command for Debugging

 "adb logcat" Command - Displaying System Logs

 "adb logcat" Command Options and Log Buffers

 "adb logcat -v" Command - Log Format Control

 "adb logcat" Command Arguments - Output Filters

 Using "adb logcat" Command to Track the Lifecycle of an Application

Java Exceptions in AndroidRuntime Error Log

 Build Process and Package File Content

 Building Your Own Web Browser

 Android Command Line Shell

 Samsung Galaxy Tab 3 Mini Tablet

 USB Debugging Applications on Samsung Tablet

 Android Tablet - LG-V905R

 USB Debugging Applications on LG-V905R Tablet

 Android Phone - LG-P925g

 USB Debugging Applications on LG-P925g Phone

 Outdated Tutorials

 References

 PDF Printing Version