例外的是NULL永远的是、NULL

2023-09-13 01:52:16 作者:噯赱ㄋ吢誶ㄋ

有人能向我解释为什么Android的抓的处理程序不会初始化一个例外对象?当错误发生时,我的code正试图初始化一个异常对象时,它始终为空。

Can someone explain to me why the Android "Catch" handler will not initialize an Exception object? When an error occurs and my code is trying to initialize an Exception object it is ALWAYS NULL.

以上没有任何意义,我因为异常应该总是初始化异常对象的时期。如果发生错误,它不应该是空的。

The above doesn't make sense to me because the exception should always initialize the Exception object period. It should never be null if an error occurs.

我是相当新的Eclipse的Andr​​oid的IDE / SDK,我相信我没有一切设定的100%。然而,这种类型的功能,似乎对我来说,它应该工作的时候,不被设置后。

I am fairly new to the Eclipse Android IDE/SDK and I am sure I don't have everything setup 100%. However, this type of functionality would seem to me that it should work all the time, not after being setup.

推荐答案

一个可能的原因是,你试图让你的主线程,其工作方式的网络连接罚款pre-2.3.3 /蜂窝(SDK水平小于10为例),但会抛出为

One possible cause is that you are trying to make a network connection on your main thread, which works fine pre-2.3.3/Honeycomb (SDK level < 10 for example) but will be thrown as an

android.os.NetworkOnMainThreadException

android.os.NetworkOnMainThreadException

由于SDK 10级。

检查:http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

问题是,Eclipse不真正了解这个异常(因为它是有条件地抛出根据不同的SDK水平,使Eclipse中可能无法获得此异常的正确实例,这解释了为什么你的异常对象总是NULL )

The problem is that Eclipse doesn't really know about this exception (since it is conditionally thrown based on different SDK level so Eclipse probably can't get a correct instance of this exception, that explains why your exception object is always NULL)

解决方案:创建一个单独的线程或使用的AsyncTask来执行你的网络连接请求

Solution: create a separate thread or use AsyncTask to perform your network connection request.