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.
Steps To Debug Android Application Error.
- In android studio, click ” View —> Tool Windows —> Android Monitor ” menu to make Android Monitor window displayed.
- 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.
- 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)
- 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.
- 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.
- 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.
- 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);
- Change the java source code and run the application again, it will be started without any error messages.
- 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 88 times, 1 visits today)