开发者

can't catch java.lang.VerifyError

开发者 https://www.devze.com 2022-12-16 02:08 出处:网络
I\'m getting this error: \"Uncaught handler: thread main exiting due to uncaught exception java.lang.VerifyError\"

I'm getting this error: "Uncaught handler: thread main exiting due to uncaught exception java.lang.VerifyError"

It's only happening on 1.6. Android 2.0 and up doesn't have any problems, but that's the main point of all.

I Can't catch the Error/Exception (VerifyError), and I know it's being caused by calling isInitialStickyBroadcast() which is not available in SDK 4, that's why it's wrapped in the SDK check. I just need this BroadcastReceiver to work on 2.0+ and not break in 1.6, it's an app in the market, the UNDOCK feature is needed for users on 2.0+ but obviously not in 1.6 but there is a fairly amount of users still on 1.6.

How to fix?

Thanks!

private BroadcastReceiver mUndockedReceiver = new BroadcastReceiver()
{
      @Override
      public void onReceive(Context context, Intent intent) 
      {
          //FROM ECLAIR FORWARD, BEFORE DONUT THIS INTENT WAS NOT IMPLEMENTED
          if (Build.VERSION.SDK_INT >= 5)
          {
              if (!isInitialStickyBroadcast()) {
                  int dockState = intent.getExtras().getInt("android.intent.extra.DOCK_STATE", 1);
                  if (dockState == 0)
                  {
                      finish();
                  }
              }
          }开发者_运维问答           
      }
};  


Intent.EXTRA_DOCK_STATE only exists in API level 5 and above, so will only work on Android 2.0 devices (or above).

Even although you're wrapping the call in an API level check, the code will fail when you run it on an Android 1.6 runtime, hence the VerifyError.

The solution would be to replace the call to Intent.EXTRA_DOCK_STATE with its constant value: android.intent.extra.DOCK_STATE.


As a general rule, it's a good idea to tick the "Filter by API level" checkbox when browsing the API documentation, and set it to 4 in your case. That way, any classes, methods or constants not available to Android 1.6 will be greyed-out.


I had a similar problem, but with bitmap scaling between 1.5 and 1.6. Ended up using something similar to the solution presented in this blog post to make a utility class that switches the code path depending on the API number.

One thing to note in that example since it supports 1.5 it uses android.os.Build.VERSION.SDK which is deprecated, but according to a Dianne Hackborn (Google engineer) it will not be removed in future SDK releases. If you only support 1.6 and above you can use android.os.Build.VERSION.SDK_INT, which is not deprecated.

Also, since you're targeting 1.6, if you aren't that dependent on that version of the framework you might want to look at supporting 1.5 as well, at the time of this writing 1.5 is 31% of Android devices accessing the Android Market.


Since it's an error rather than an exception, it won't extend Exception. Rather, it will extend Throwable, so you have to catch that:

try {

....

} catch ( Throwable e ) {
}


To catch the error you can do something like this.

try {

  // write your code here

    } catch (VerifyError e) {
        // TODO: handle exception
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消