tags:

views:

404

answers:

3

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();
                  }
              }
          }           
      }
};  
A: 

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 ) {
}
Eric Petroelje
that didn't work, thanks
velazcod
+3  A: 

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.

Christopher
of course, makes sense.However, I replaced it with the constant value like this and I'm still getting the VerifyError.intent.getExtras().getInt("android.intent.extra.DOCK_STATE", 1);
velazcod
`isInitialStickyBroadcast()` is also only available on Android 2.0+, so you could see whether you can do without that call.
Christopher
that's correct, that's why i'm wrapping it all in the SDK check... it would suck if this wasn't possible to fix.
velazcod
isInitialStickyBroadcast() needs to be called since ACTION_DOCK_EVENT is a sticky broadcast.
velazcod
I know which APIs are available in SDK 4 and what is not, I know Intent.EXTRA_DOCK_STATE and isInitialStickyBroadcast() got introduced in SDK 5.What I'm trying to achieve here is to be able to post an app on the market with this SDK 5 dock support, but that it doesn't break for users on 1.6.
velazcod
If you need to use APIs that aren't available and can't just use constants, then check out the advice on using reflection on the Android Developers' site: http://developer.android.com/resources/articles/backward-compatibility.html
Christopher
yes, using reflection worked, thanks!
velazcod
+2  A: 

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.

wf