2

我正在学习如何使用 ABS。该代码在 4.1 上运行良好,但是当我在 2.2 上运行相同的代码时,它抛出异常

这是 MainActivity.java

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;

public class MainActivity extends SherlockFragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ab.addTab(ab.newTab().setText("Beauty").setTabListener(new Listner()));
        ab.addTab(ab.newTab().setText("Cookery").setTabListener(new Listner()));
        ab.addTab(ab.newTab().setText("Health").setTabListener(new Listner()));
        ab.addTab(ab.newTab().setText("Relationship").setTabListener(new Listner()));
        ab.addTab(ab.newTab().setText("Yoga Spirituality").setTabListener(new Listner()));
    }

    class Listner implements TabListener {

        public void onTabReselected(Tab tab, FragmentTransaction ft) { }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            ft = getSupportFragmentManager().beginTransaction();

            switch (tab.getPosition()) {
                case 0:
                    ft.add(android.R.id.content, new BEAUTYFragment());
                    ft.commit();
                    break;

                case 1:
                    ft.add(android.R.id.content, new COOKERYFragment());
                    ft.commit();
                    break;

                case 2:
                    ft.add(android.R.id.content, new HEALTHFragment());
                    ft.commit();
                    break;

                case 3:
                    ft.add(android.R.id.content, new RELATIONSHIPFragment());
                    ft.commit();
                    break;

                case 4:
                    ft.add(android.R.id.content, new YOGAFragment());
                    ft.commit();
                    break;

                default:
                    break;
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) { }
    }
}

这很明显:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sher"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    </application>

</manifest>

LogCat 中的错误:

java.lang.NoClassDefFoundError: com.actionbarsherlock.R$styleable

4

1 回答 1

2

您需要设置 ABS 主题或让您的主题从 ABS 主题扩展。需要处理 ActionBar 的向后兼容性(3.0 之前的设备)

例如

<application
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/Theme.Sherlock.Light.DarkActionBar">

以下是有关ABS 主题的一些信息

于 2013-01-26T21:15:36.870 回答