我的应用程序中有此源:

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        setTheme(getHoneycombStyle());
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setCustomView(R.layout.ACTION_BAR);
        }
    }
}

它在Honeycomb、Android2.1、2.2和2.3中运行良好,但在Android1.6中不起作用。虽然我已经检查了sdk版本,但应用程序返回以下异常:
08-31 18:23:33.063: ERROR/AndroidRuntime(3262): Uncaught handler: thread main exiting due to uncaught exception
08-31 18:23:33.083: ERROR/AndroidRuntime(3262): java.lang.VerifyError: com.eldan.introducing.activity.IBBaseCompActivity
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at java.lang.Class.newInstanceImpl(Native Method)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at java.lang.Class.newInstance(Class.java:1472)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.app.Instrumentation.newActivity(Instrumentation.java:1097)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2242)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:631)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.widget.TabHost.setCurrentTab(TabHost.java:317)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:127)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:346)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.view.View.performClick(View.java:2344)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.view.View.onTouchEvent(View.java:4133)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.view.View.dispatchTouchEvent(View.java:3672)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:850)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.os.Looper.loop(Looper.java:123)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at android.app.ActivityThread.main(ActivityThread.java:4203)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at java.lang.reflect.Method.invokeNative(Native Method)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at java.lang.reflect.Method.invoke(Method.java:521)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
08-31 18:23:33.083: ERROR/AndroidRuntime(3262):     at dalvik.system.NativeStart.main(Native Method)

听起来编译器在myActionBar方法中检测到了onCreate源代码,android 1.6不支持它。但我不太确定如何解决这个问题。

最佳答案

不能在加载到旧版本android的vm中的代码中引用较新版本android的类或方法。
你有两个主要的选择:
ActionBar代码隔离到一个单独的实用程序类中,并且仅当您在Honeycomb上时才使用该类,具体由android.os.Build确定。Here is a sample project演示如何使用getActionView()将自定义布局放入操作栏,该操作栏适用于Honeycomb和旧版本,方法是在一个getActionView()类中隔离HoneycombHelper调用,该类只在Honeycomb上使用过。
使用ActionBarSherlock访问操作栏。它将为您提供蜂巢上的本地操作栏,以及Android旧版本上的类似功能。

08-18 06:31