App crashes, but doesn't crash - Java for Android App Development

First of all I know that this title creates a lot of questions.
The problem is exactly what the title says.
While developing apps, you make mistakes (NullPointers etc)
They are thrown when the occur, normally.
Today, it's different, sometimes, when Android (Studio) feels like it wants to help me, it redirects the debugger to a class that is used to "crash" the app at an exception.
With the cool features of Android Studio, you are able to see the content of the strings during debugging. With that feature, I've been able to solve most of the problems.
But now, I ran into a problem that doesn't have the solution above.
When the app "crashes" the screen stays white, and nothing is outputted in any logcat.
All it says is
"D/AndroidRuntime: Shutting down VM"
Is this a bug in Android Studio or in Android?

Hello,
First of all make sure that you have the latest version of Android Studio.
When the app crashes is selected application above the logcat (there is a spinner which says you what app is being displayed, does it say 'com.yourpackagename'? or does it say 'No Application'?) If so I think there is a bug in Android Studio, I have faced it too several times. When your app crashes it force closes the app?
You can try these things:
Unplug your phone and plug it again and wait for a confirmation on the device's screen.
If the confirmation dialog is not showing up, while the device is connected via USB, go to the Settings and disable the USB debugging mod. Wait a few seconds and re-enable it. Wait for confirmation on the device. If still not shows. Close the Android Studio and repeat the above steps. Reopen the Android studio. Run your app, if still not getting logcat you can try getting it manually like this:
Go to the folder where adb is installed (Normally on Windows C:/Users/Username/Appdata/Android/sdk/platform-tools) via terminal with admin rights .
Run cmd.exe as admin, type:
cd ../../Users/Username/Appdata/Android/sdk/platform-tools (where Username is the username of your windows account.)
Type adb devices while your phone is connected via USB with debugging on
See if it says unauthorized, if not, you are good to proceed to the next step.
Type
adb logcat > mylogcat.txt
And quickly open your app to go to the app that causes the crash. Try to cause the bug. When the bug occurs press Ctrl+C
Now your logcat is in the mylogcat.txt within the same folder of adb.
If you need more help extracting the logcat via the above way (manually) let me know

Hmm, at the moment the crash occurs, the app freezes and stays white. Only the main thread though. I've checked the logcat in Android Studio, the app is my app, and the doesn't really matter. It shows nothing.
No logcat not even with the cmd option.
Could it be that my custom rom is causing difficulties?
Thanks,
Tim

Try to place breakpoints in every line on your code. Try step by step to run the app line by line and where the execution flow jumps some lines of code that it shouldn't or stucks at a line of code whithout going to the next lines of code that it should, there should be the problem.
Are you doing intense work on the main thread? I had experienced an issue where I was doing heavy work on a view's onTouchListener and because the touch events were bottlenecking, my UI stuck, and on logcats I didn't got any error, but got a warning that the thread is shutting down due to the fact that the onTouchListener was busy to handle the touch event.
The best option and the most reliable you have now is to debug your app line by line as mentioned above. If it does not help, try commenting lines of code where you think you may be doing intense work or inside listeners and see if your app still crashes. Then uncomment blocks of code and run again and again to see until it crashes again. In the last block of code that you uncommented should be the error.
That way you can limit your search on these lines of code.
If you could further provide any detail of what are you doing in the main thread or any sample code, I will be able to help you further
I do not think that it is related with the custom rom, otherwise logcats would have some errors of missing packages etc

Well, using breakpoints I was redirected to the piece of code that is used to output exceptions. I didn't had a breakpoint there so I don't know why it went that deep. I'm doing heavy for with the runtime.exec command.
I've discovered the exception that was thrown when I posted this question, and the exception wasn't printed anywhere so I don't know why this isn't working. I was getting the java.lang.NumberFormatException exception. This will cause the app to crash, right?
If not how do I 'enable' that?
Thanks

If you are catching that exception the app will not crash. (try catch block). It may crash when any variable from that try catch block had that Number format exception, but when referencing it from outside the try-catch block.
Numberformat exception is occured when trying to convert String to Integers or a Number, and the string does not match the Integer format. E.g. Integer.parseInt("4.3") will throw that exception.
To enable the crashing of the app during that exception, do not catch that exception (NumberFormatException). If you are catching Exception (it is a superclass of NumberFormatException and is more general, it includes this exception too) then again your app will not crash.
If you are catching any of these exceptions, your app will not crash (may crash inside the catch or outside the try catch block)
Exception
RuntimeException
IllegalArgumentException
NumberFormatException
Possibly, a string conversion to Integer or Long or Double or Float may be causing your problem The errors are "silenced" due to the try-catch block Are you printing the exceptions with e.printStrackTrace in the first line of the catch block? (e is the Exception caught)

Hmm, I'll take a look at my code so check if I used a try and catch block.
Thanks!

It was the fault of Google Analytics, I had exception reporting on and that was causing the app to freeze instead of crashing and showing the exception.
Thanks!!

Related

Identifying symlink to avoid "infinite" loop when browsing files.

Hi,
I have tested this behaviour on both a N1 and the emulator. Could anyone tell me if that is a problem? How can I avoid the recursive code going forever? What is happening?
If you use any file browser/manager and starting going into /sys/devices/w1 bus master/subsystem/devices/w1 bus master/subsystem it will go inner forever, repeating that pattern devices/w1 bus master/subsystem/ all the way.
So when I run a recursive search it keeps going deeper and deeper and seems never to finish.
What is happening, how to avoid this behaviour?
I tried different file managers and they all seem to be going forever into these folders, although when I do a search through Astro it doesn't seem to get stuck with this loop (It seems to ignore the /sys folder, but I am not sure if this happens also in any other folder).
Some people point me this problem can be related to symlinks, but I didn't find a way of checking for those symbolic links with Android's Java.
I know it can be done (as proved by Astro file manager), I just don't know how.
Hopefully it won't involve any call to functions which I'll have to create using NDK, which would be a pain.
Regards.
astro goes infinite for me on an N1
I think any app needs to do some extra checks instead of following stuff down.
eg the "find" command has the "-follow" option
open a terminal or use "adb shell" and "cd /sys/bus"
now type in "find" and it will display all the files directories and symlinks without following them
now type in "find -follow" - you will need to press Control-C to get out, you will see that each line has an error "too many symlinks" - kernel has protection/limit for symlink levels.
writers of file managers could easily fix this, but you have to ask what you were doing in their anyway?
Lol, I was trying to find some info to address this problem under Android & found a relevant thread I thought might help, only to discover it was you (jfbaro) having this conversation on another forum
Specifically, this thread on anddev.org.
I don't know Android yet, but a getCanonicalPath() like call is always going to be at the heart of spotting in advance & avoiding this type of problem, whatever your environment & language. If it doesn't work either the function is broken or you're making a mistake somewhere, I'm fairly sure.
In the above thread you say this doesn't help you. Can you post what getCanonicalPath returns for both /sys/device/subsystem/ & /sys/device/subsystem/sys/ ?
Let's assume you start your traverse at / & have reached /sys/device/subsystem/. When you check to see if it's safe to follow /sys/device/subsystem/sys you should discover that the latter is actually /sys, and that further, it is in your list of already scanned directories, hence you do not search further down that branch.
Where is this breaking down?
[Edit:] You might also find something of use buried in this bug thread which involves similar issues. Funnily enough it concerns Eclipse, but all that matters is that it's a Java based example of the problem. From a quick scan, getCanonicalPath again seems to be the solution though I think they avoid any performance hit by only using it on files known to be sym-links.

[Q] Android app auto starts again when device rotated

Hello all,
I've written a simple audio player app and testing it in Samsung Galaxy S.
It is a simple app with can create playlist and some buttons to play the mp3 file.
However, there's a funny bug in it.
After starting the app, whenever I rotate the phone, the running app will launch another instance of the app. If I rotate again, another instance (3rd) would be started. So u can hear 3 copies of the same audio being played simultaneously.
What could be wrong with it?
Is there some code which can prevent program re-entry?
Thanks.
It's not a bug, that's the way andorid OS works. You need to account for this in OnCreate for your activity. I assume you are spawning off the actual playing of the MP3 in a thread or service. You need to check if the service is already running in OnCreate and attach, else spawn a new one.
Hello Gene,
How do I check if an activity is already running?
I could not find the answer on the android developer page (the topic on application fundamentals).
And no, I'm not converting it to a service yet as this is my first app.
Haven't explore service yet.
Thanks.
I read on another article, a simple way to prevent this is to add overwrite the onConfigurationChanged function
@Override
public void onConfigurationChanged(Configuration newConfig) {
//ignore orientation change
super.onConfigurationChanged(newConfig);
}
and modify the androidmanifest.xml
<activity android:name="selectCategories" android:configChanges="orientation|keyboardHidden"></activity>
But it still launch multiple instance of the app.
Thanks.
This is a good question and should be in an FAQ somewhere.
As already mentioned, changing the display orientation basically restarts your app. Read the Android dev page on Activity lifecycle for more info.
A short answer for your question is: the Bundle parameter for onCreate() will be null when your app is first run. When your app is paused and restarted, that Bundle will be non-null. You can store data in that Bundle by overriding onSaveInstanceState(), then check for that data in onCreate(). It's a good idea to learn how to do this (save/read app data on pause/restart). Once you start testing apps by rotating the display at various times, you'll find a lot of them FC at unexpected places.
This is indeed a topic that keeps surprising people who are new to android (ahem, like me 4-5 months ago ).
The solutions above are perfect, however in certain situations there's another trick that might make your life much easier. I suspect it won't help you in this case, but it might help others who tackle the problem and see this thread.
Certain applications, mainly games, should be fixed in a single orientation. I.E. you won't be playing angry birds on portrait - that game is locked to landscape, as it should be. In order to lock your activity in a certain orientation you add this attribute to the manifest under the Activity tag. So a standard activity might look something like that, combined with the ignore tag from the previous posts:
Code:
<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden"
android:name="whatever">
The great thing about this combination is that your activity will not restart at all when the phone is rotated. Of course, for a standard activity you'd usually want to support both landscape and portrait, but if you need your app to be in a fixed orientation this is the way to go - no weird FCs or annoying bugs
Hi,
How do I check the bundle?
And also, what to do if the bundle is not null? Just return?
Thanks.
regards
r_p_ang said:
This is a good question and should be in an FAQ somewhere.
As already mentioned, changing the display orientation basically restarts your app. Read the Android dev page on Activity lifecycle for more info.
A short answer for your question is: the Bundle parameter for onCreate() will be null when your app is first run. When your app is paused and restarted, that Bundle will be non-null. You can store data in that Bundle by overriding onSaveInstanceState(), then check for that data in onCreate(). It's a good idea to learn how to do this (save/read app data on pause/restart). Once you start testing apps by rotating the display at various times, you'll find a lot of them FC at unexpected places.
Click to expand...
Click to collapse

[Q] Debugging help needed

I have a user of my app who is having a problem running it. My code launches another activity in the same app, and he is saying it is stopping before it should & returning to the previous activity, and he doesn't see any Force Close warnings.
I have run my code in the emulator & on my phone, I can't reproduce the error. We both run Android 2.2 on our phones, his is an HTC EVO & mine is a HTC Wildfire, as far as I can tell his specs are better than mine so shouldn't cause an issue - I deliberately chose a low spec for for my dev work so the code ought to run on anything.
As a bit of an Andoid dev noob (but been coding for years), is there any easy way I can make a special build of the app to send to him that would log any errors that happen ? I'd like to get a stack dump as well if possible, as I'm not sure exactly what routine in the activity its crashing out in. The activity that crashes is Gallery with 9 images in it, he can't flick through them or select one. I'm stumped as to whats causing it, any assistance would be gratefully received.
Thanks.
Why not point to your app and let others here try it on their phones? It could simply be other apps installed on his phone interfering with your app.
Long time programmer here too and when I get to where you're at (and I"m sure you've put some hours into this LOL), I go back to STEP 1.
I comment-out any and all code but the bare minimum; break it down to the Intent, startActivity and maybe a Toast message in the second activity. Even parse down your XML files to bare minimum.
See if that works. Then, ADD BACK ONE LINE OF CODE AT A TIME Run program and make sure it works. Yeah, it's painful, but in my 20 years of coding, I've learned to put my pride aside and to not "pretend" all the code I've written is correct.
Sometimes on bigger projects, I"ll change or add a couple of lines of code, run a back up and test. Rinse and repeat LOL. That way, I know I"m only a couple of lines of code from what "used" to work.
Good Luck!
Thanks both of you.
old_dude - Its a paid app. Only £0.99 but I don't think people would pay to help me. There is a free version of the same app (with less functionality) that this guy can get to work. If your really interested the 2 versions are -
Plink Log - Free Version
Plink Log Pro - Paid version
Rootstonian - agreed thats the approach I'd normally take if I was having problems on my dev phone or the emulator. The problem is that its OK on my HTC Wildfire/Android2.2 but on this guys HTC EVO/Android2.2 its having problems. I dont really want to keep sending him .apks with 1 or 2 lines extra enabled just to see if that fixes his specific issue. I was hoping there was something I could code to catch whatever crashes the activity & log it somewhere for me to analyse. When I do PC dev work, I have a global exception handler that catches anything I dont explicitly handle, and dumps the full call stack into a Log File I can read later.
I think I'll just have to take the existing app & put loads of debug code into it to save messages into a log file & see what bits of code are being called & what isn't & then get him to email me the results.
Thanks for the ideas guys, its always useful to get input from another perspective.
Dave
Hmmmm, just discovered setDefaultUncaughtExceptionHandler - might be able to use that with printStackTrace. Sounds interesting.

[APP][1.5+] Remote LogCat 1.08 - device/browser/sd/http/sms logcat

You did read the title and still clicked so I think you may need this little app
I wanted to see the Android device log convenient on my desktop browser without using USB cable and also be able to send APKs to the device to update my apps. Without root! App install of course needs confirmation on device.
Also a nice log view on the device is always helpful - I found aLogCat scrolling too slow to be usefull and it can not jump directly to errors.
For tests of my remotely running app MobileWebCam I also need the possibility to get logs saved even without internet connection - so SMS can be sent to the device to trigger logging. For other apps there is an intent to trigger logs if Remote LogCat is installed. And the standard feature is there, too: dump logs from time to time onto sdcard, http or others like email with the support of my other app AutoShare ...
Filtering the log is a little complicated right now but I found myself using the full log most of the time anyway ...
Find the apk is attached to this post or in Android Market.
Nice. I will try it when I get time. It seems very useful.
Ohh, for some time I've wanted something like this! Thank you _miha_!
Downloading now, will report a little bit later as my phone is currently in 'flashing' state!
Thanks _miha_! Working great on my LG Thrill 2.3.5. I would love to see it auto scroll on the pc browser side. Even without it, awesome app. Thanks again!
Wow this is work great already do a test from Browser & everything is Great
Nice
It does not work for me, because the browser URL shows up like this:
http://fe80::123:65ff:fe68:767a%wlan0:8080
Why is that?
I am using a SGS2 with CM9.
Also, the RemoteLogCat service is running all the time, even when the app is closed....
CRXed said:
http://fe80::123:65ff:fe68:767a%wlan0:8080
Why is that?
Click to expand...
Click to collapse
That is an IPv6 format address. I do not know why you have one - maybe CM9 uses v6 preferred?
Try to enter [ and ] around the numbers and ::s. Like:
http://[fe80::123:65ff:fe68:767a%wlan0]:8080
(see here: http://superuser.com/questions/242122/accessing-non-port80-web-server-using-ipv6)
You can disable webserver with the third setting (Enable - HTTP server enabled. Access using Browser...) - it is enabled default at the moment so users do not have to search it before trying the app.
I can't get it to work.
My phone has a normal IPv4 IP adres, 192.168.0.13
I tried
http://[fe80::123:65ff:fe68:767a%wlan0]:8080
http://[fe80::123:65ff:fe68:767a]:8080, because, what is %wlan ??
Anything else I can try?
Also, when disabling HTTP server and exiting the app with the back button, the logcat service is still alive.
version 1.04
CRXed said:
Anything else I can try?
Also, when disabling HTTP server and exiting the app with the back button, the logcat service is still alive.
Click to expand...
Click to collapse
Updated version 1.04 displays ipv4 adress only now. And does no longer let Android restart the service in case it is not enabled (thanks for making me aware of that)!
IPv4 is working now!
But when I disable the HTTP server, I still see a service running in the background.
Which service is this? I use SystemPanel to check.
Also, when opening the app for the first time, my entire phone get's very slow, because it is trying to retreive all logging info.
After clearing the log, everything is smooth again.
Also, when the log is very big, I get this:
Code:
W/dalvikvm(18755): threadid=21: thread exiting with uncaught exception (group=0x40a541f8)
E/AndroidRuntime(18755): FATAL EXCEPTION: Thread-333
E/AndroidRuntime(18755): java.lang.OutOfMemoryError
E/AndroidRuntime(18755): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
E/AndroidRuntime(18755): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
E/AndroidRuntime(18755): at java.lang.StringBuilder.append(StringBuilder.java:216)
E/AndroidRuntime(18755): at com.dngames.remotelogcat.a.a(HelloServer.java:85)
E/AndroidRuntime(18755): at com.dngames.remotelogcat.h.run(NanoHTTPD.java:424)
E/AndroidRuntime(18755): at java.lang.Thread.run(Thread.java:856)
W/ActivityManager( 1803): Force finishing activity com.dngames.remotelogcat/.RemoteLogCat
More info in my signature.
To stop the always running background service please disable in the settings and leave the app. This seems not to be obvious enough - so maybe I should add a button in the first menu?
After a long time a little update to version 1.08 ...
I stripped html from the browser logcat so it is no longer confused when apps log html tags. Also two error message crashes are fixed.

Help with running a backscreen sample

I had an idea to do a time tracking program using the back screen, so thought I'd knock it up and see how it is. But no, not so simple.
For background, I've actually released a few games on Android, but they were a few years ago and were all in C++. They only used Java to open a GL context and connect to input and audio. The build system was a custom one and I didn't go near Eclipse.
So I downloaded Android Studio, made a copy of the yp_hello project and got it all building and running. Except that it crashes on startup every time.
This:
setBSContentView(R.layout.bs_activity_main);
is immediately followed by this:
mTransferredText = (TextView) (findViewById(R.id.bs_sub_text));
But it crashes here with java.lang.ClassCastException: java.lang.reflect.ArtMethod cannot be cast to android.widget.TextView
However, it doesn't crash if I put in a breakpoint in and run it.
Without the breakpoint, a Log print shows that findViewById returns an java.lang.reflect.ArtMethod object.
With a breakpoint anywhere in the code (even after the crashing line), the same Log print show that findViewById returns an android.view.TextView object. Once the breakpoint has been hit, I can continue the program and it all works fine on the phone.
Can anybody help with getting this working? It's just a yp_hello sample that comes with the Yotaphone SDK, made to run in the latest Android Studio with the latest SDK.

Categories

Resources