Debug Android App With Android Studio Examples

When you execute an Android app after coding, you may always meet bellow error in device simulator. The simulator popup a dialog to tell you the android application has been stopped. If you are a newbie of android development, you may become very confused why this happened, what’s wrong with the code. Do not worry, this article will tell you how to debug the error.

android app runtime error

Steps To Debug Android Application Error.

  1. In android studio, click ” View —> Tool Windows —> Android Monitor ” menu to make Android Monitor window displayed.
    android studio show android monitor window
  2. Now you can see the Android monitor is displayed at bottom. Please make sure you choose “Warn” and “No Filters” selection in logcat tab as below.
    logcat choose warn and no filters selection
  3. Click Run button in top tool bar to run the application again, a lot of messages will be shown in the logcat window. You may find there is a java.lang.NullPointerException, you can click the java file link to navigate to the source code where the exception happened.
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
     at com.dev2qa.example.activity.SwitchScreenActivity1.onCreate(SwitchScreenActivity1.java:26)

    logcat console show java nullpointer exception error message

  4. If you feel the logcat window is not big enough, you can click settings icon at top right corner in Android monitor window, then click ” Resize —> Maximize Tool Window ” to make logcat window maximize.
    maximize android monitor logcat output window
  5. After click the java file link beside the exception in logcat window, you can go to the error happened source code. Click the column at the beginning of the code line to set a break point.
    set break point in android studio
  6. Click debug button, the execution will stop at the break point. From the debugger window at bottom, you can see that btnToScreen2 object is null in this example. You can also mouse hover the btnToScreen2 object in java source code to find that it is null.
    watch variable value in android studio debugger
  7. Now you can find the error reason is wrong button id used, you should use id R.id.btnGotoScreen2 to replace R.id.btnGotoScreen1.
    Button btnToScreen2 = (Button)this.findViewById(R.id.btnGotoScreen1);
  8. Change the java source code and run the application again, it will be started without any error messages.
    android example switch between activity screen one
  9. You can find the example source code in article Android Activity Example – Switch Between Screens

can not bind to local 8616 for debugger

If you meet can not bind to local 8616 for debugger error during android app debug process use android studio,  you can run below shell command to fix it. Then you can debug your android app as normal.

adb kill-server 

adb start-server
(Visited 133 times, 1 visits today)
READ :   Android Drag And Drop Example

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.