我正在尝试让我的 Android 应用在较低版本的 Android 上支持 ActionBar,所以我使用的是 ActionBarSherlock 4.2.0。我还使用同一个开发人员的“NotificationCompat2-1.1.2”来获得更好的通知支持。
所以该应用程序有 ActionBar 选项卡,我正在使用 ActionBar.TabListener。我的实现如下所示:
public static class TabListener<T extends SherlockFragment> implements ActionBar.TabListener {
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
private final Bundle mArgs;
private android.support.v4.app.Fragment mFragment;
public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz) {
this(activity, tag, clz, null);
}
public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz, Bundle args) {
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached()) {
FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
ft.detach(mFragment);
ft.commit();
}
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
ft.add(android.R.id.content, mFragment, mTag);
} else {
ft.attach(mFragment);
}
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
}
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
//Do nothing
}
}
我的片段扩展了 SherlockFragment 类。
所以这一切都很好,除非我尝试在不原生支持 ActionBar 的设备上运行它,即低于 3.0 的 Android 版本。因此,ActionBarSherlock 似乎无法正常工作,无法正常工作。
我得到的错误是:
错误/dalvikvm(330):找不到类 'com.shockwave.clockproj.StopwatchFragment',引用自方法 com.shockwave.clockproj.ClockMain.onCreate 11-16 16:23:50.165:错误/AndroidRuntime(330):致命例外:主要
java.lang.NoClassDefFoundError: com.shockwave.clockproj.StopwatchFragment at com.shockwave.clockproj.ClockMain.onCreate(ClockMain.java:20) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) at android.app.ActivityThread.access$1500(ActivityThread.java:117) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:3683) 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:839) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method)
出现错误的行是实例化选项卡的位置,如下所示:
ActionBar.Tab stopwatchTab = actionBar.newTab().setText("Stopwatch").setTabListener(new TabListener<StopwatchFragment>(this, "Stopwatch", StopwatchFragment.class));
所以显然它找不到片段的类定义,但只能在 Android 2.3 或更低版本上找到。我该如何解决?