Android catching Exceptions - android

I have an exception that I'd like to handle, but I cannot tell its origin based on the stack trace (NsdManager.java:338 isn't mine and that files only has 58 lines). None of the files it references are mine.
Is there a way to handle any exception that comes up from the Handler?
E/AndroidRuntime﹕ FATAL EXCEPTION: NsdManager
java.lang.NullPointerException
at android.net.nsd.NsdManager$ServiceHandler.handleMessage(NsdManager.java:338)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.os.HandlerThread.run(HandlerThread.java:60)
I am using a handler to help discover/rediscover a network based service when WiFi switches on and off.
Edit: Pastie of code
Full Stack Trace
MainActivity
NsdHelper
WifiBroadcastReceiver
WsHelper
NsdManager - Line 338 doesn't exist here, so I don't know what code to wrap in a Try/Catch.
Thanks

Turns out the NSD Manager has some long standing problems
https://code.google.com/p/android/issues/detail?id=35585
jmDNS works much better.

Related

What does sCount=2 dsCount=1 mean in android ANR file?

I have some problem with an ANR files, I got a SUSPENDED lable in main thread,
somebody say the reason maybe GC , Debugger, infinite loop, so I have try Debugger and infinite loop, and I got the SUSPENDED , but the difference is when I use Debugger to get ANR, I got sCount=2 dsCount=1, and when I use infinite loop to get ANR, i got sCount=1 dsCount=0..
so What does it mean in an ANR file?
Following is the screenshot of the ANR
As you can judge from sources:
dvmPrintDebugMessage(target, " | group=\"%s\" sCount=%d dsCount=%d s=%c obj=%p
self=%p\n",
groupName, thread->suspendCount, thread->dbgSuspendCount,
thread->isSuspended ? 'Y' : 'N', thread->threadObj, thread);
So, to answer your question:
sCount stands for thread->suspendCount, dsCount stands for thread->dbgSuspendCount.

Error while creating a new activity in Android Studio

Every time I create a new activity in Android Studio, I get an error like this:
Read access is allowed from event dispatch thread or inside read-action only (see com.intellij.openapi.application.Application.runReadAction())
Details: Current thread: Thread[ApplicationImpl pooled thread 17,4,main] 1345722054;
dispatch thread: false; isDispatchThread(): false
SystemEventQueueThread: Thread[AWT-EventQueue-0 1.3#AI-141.2178183, eap:false,6,main] 2033877427
It doesn't cause any problem though, and the activity is created like it should normally be. So what is the problem?
There is no evidence that this log is related to your application.
it has something to do with android studio (intellij) which you can easily ignore as long as everything is working fine for you.

Apache Mina: IllegalStateException when calling connect

I am trying to make a simple message application on Android with Apache Mina. I am getting IllegalStateException when calling connect(new InetSocketAddress(HOSTNAME, PORT)).
Here is my client code:
http://pastebin.com/NR2H6X0t
Here is my server code:
http://pastebin.com/Q5fQnu2p
And here is the logcat output:
http://pastebin.com/Egsh0Ce4
I am testing my apllications in same wifi network with a tablet and phone.
Even I can't see anything wrong with your code. But the LogCat says org.apache.mina.transport.socket.nio.NioSocketConnector.connect() of NioSocketConnector.java class at line# 185) your application is throwing NetworkOnMainThreadException. First fix that than check if you are getting the same error.
Apart from above I have a suggestion (for readability purpose): In connectToServerButtonClicked() of ClientActivity instead of using
for (; ; ) {
//rest of your code
}
better use
while(true){
//rest of your code
}
Edited: On request of #alkis coping my answer from comment below to answer part for better redability
NetworkOnMainThreadException : This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your network related code in AsyncTask.

My app shuts down and says "unfortunately <appname> has stopped working" I'm very new to programming . Help much appreaciated

THIS IS MY LOGCAT
09-18 17:19:50.770: D/AndroidRuntime(797): Shutting down VM
09-18 17:19:50.770: W/dalvikvm(797): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-18 17:19:50.800: E/AndroidRuntime(797): FATAL EXCEPTION: main
09-18 17:19:50.800: E/AndroidRuntime(797): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thenewboston.honey/com.thenewboston.honey.StartingPoint}: java.lang.NullPointerException
Not sure if posting a code snippet would be helpful in debugging.
Check out line 26 of your onCreate method in StartingPoint.java.
You are experiencing a NullPointerException rooted from whatever is on that line.
As other comments have suggested, a more detailed answer would require your code (specially the contents surrounding line 26)
This isn't a complete answer without code but since you are new it is very important for you to know how to read the logcat when you have a crash. This will allow you to debug the simpler problems yourself and will save on a few headaches occasionally.
First part is to find the line which says Fatal Exception like here
09-18 17:19:50.800: E/AndroidRuntime(797): FATAL EXCEPTION: main
then look for Caused by:
09-18 17:19:50.800: E/AndroidRuntime(797): Caused by: java.lang.NullPointerException
this tells us that the root cause was a NPE (NullPointerException). Now look for the next line after that which references your project and that will give you the Activity, function, and line number. Here it happens to be the next line
at com.thenewboston.honey.StartingPoint.onCreate(StartingPoint.java:26)
this tells us that it is happening in StartingPoint.java in the onCreate()at line 26. So you know to check that line, maybe set a breakpoint and see what is null then from there you can figure out why it is null. And when you can't figure out why then you can post the logcat and most relevant code here. In this sitaion it would be the onCreate() method of that Activity.
Reading the logcat isn't always this simple but many times it will be.

Leak found: AndroidHttpClient created and never closed

I don't really instantiate an AndroidHttpClient anywhere in my code, but I do start a recognizer intent, which yields this exception at some point when my application runs:
Leak found
java.lang.IllegalStateException: AndroidHttpClient created and never closed
at android.net.http.AndroidHttpClient.<init>(AndroidHttpClient.java:152)
at android.net.http.AndroidHttpClient.newInstance(AndroidHttpClient.java:138)
at com.google.android.voicesearch.speechservice.SpeechServiceHttpClient.<init>(SpeechServiceHttpClient.java:59)
at com.google.android.voicesearch.speechservice.ServerConnectorImpl.<init>(ServerConnectorImpl.java:85)
at com.google.android.voicesearch.VoiceSearchContainerImpl.createRecognitionController(VoiceSearchContainerImpl.java:83)
at com.google.android.voicesearch.GoogleRecognitionService.onCreate(GoogleRecognitionService.java:65)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2066)
at android.app.ActivityThread.access$2500(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Assuming the bug is not in Android's code but in mine, this leaked resource could have been created only in one of the following:
SpeechRecognizer.createSpeechRecognizer()
new RecognitionListener()
new Intent(SpeechRecognizer.RESULTS_RECOGNITION)
SpeechRecognizer.startListening(recognizerIntent)
But how do I know which one?
Assuming the bug is not in Android's code but in mine
The leak is in com.google.android.voicesearch, which is not your code.
If you can create a small sample project that demonstrates this leak, we can get an issue over to Google and hope that they will address it.
Why do you assume it's not in Android? SpeechRecognizer and RecognitionListener are in ASOP, so you can check (it's not there). Most probably SpeechServiceHttpClient uses an AndroidHttpClient internally. There is not much you can do but check you are calling all finalizer methods, if any (close(), shutdown(), etc), of the framework classes you are using.

Categories

Resources