带有Actionbarcompact 2.3.3的Actionbar选项卡

时间:2014-01-09 08:10:36

标签: android android-fragments android-actionbar android-support-library android-actionbar-compat

我正在使用v7和v4库构建一个带有actionbartabs的应用程序,我遇到的问题是我无法点击Android 2.3.3模拟器上的任何标签,但可以在4.x +上正常工作。他们总是把我的错误夸大布局。

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;


ViewPager mViewPager;

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

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {

        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        //return PlaceholderFragment.newInstance(position + 1);
        switch (position) {
            case 0:
                // About Rated fragment activity
                return new AboutFragment();
            case 1:
                // Achievements fragment activity
                return new AchievementFragment();
            case 2:
                // Constituency fragment activity
                return new ConstituencyFragment();

            case 3:
                // Party fragment activity
                return new PartyFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
            case 3:
                return getString(R.string.title_section4).toUpperCase(l);
        }
        return null;
    }
}

以上代码在2.3.3模拟器上构建并运行顺畅,但是滑动以访问下一页(或)单击其崩溃并退出的任何其他选项卡 这是我的简单派对片段,它只返回一个textview

public class PartyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_party, container, false);
    return rootView;    
 } }

这是我的堆栈跟踪

    01-09 13:21:51.377: D/dalvikvm(365): GC_EXTERNAL_ALLOC freed 80K, 51% free 2755K/5511K, external 7190K/8940K, paused 40ms
    01-09 13:21:51.407: E/dalvikvm-heap(365): 6401360-byte external allocation too large for this process.
    01-09 13:21:51.417: D/dalvikvm(133): GC_EXPLICIT freed 19K, 50% free 2957K/5895K, external 2793K/3250K, paused 715ms
    01-09 13:21:51.447: E/GraphicsJNI(365): VM won't let us allocate 6401360 bytes
01-09 13:21:51.447: D/dalvikvm(365): GC_FOR_MALLOC freed 1K, 51% free 2753K/5511K, external 7190K/8940K, paused 23ms
01-09 13:21:51.447: D/skia(365): --- decoder->decode returned false
01-09 13:21:51.457: D/AndroidRuntime(365): Shutting down VM
01-09 13:21:51.457: W/dalvikvm(365): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-09 13:21:51.487: E/AndroidRuntime(365): FATAL EXCEPTION: main
01-09 13:21:51.487: E/AndroidRuntime(365): android.view.InflateException: Binary XML file line #3: Error inflating class <unknown>
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.view.LayoutInflater.createView(LayoutInflater.java:518)
01-09 13:21:51.487: E/AndroidRuntime(365):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-09 13:21:51.487: E/AndroidRuntime(365):  at com.umidtech.tabbacwardcompatible.ConstituencyFragment.onCreateView(ConstituencyFragment.java:25)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.os.Handler.handleCallback(Handler.java:587)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.os.Looper.loop(Looper.java:123)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-09 13:21:51.487: E/AndroidRuntime(365):  at java.lang.reflect.Method.invokeNative(Native Method)
01-09 13:21:51.487: E/AndroidRuntime(365):  at java.lang.reflect.Method.invoke(Method.java:507)
01-09 13:21:51.487: E/AndroidRuntime(365):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-09 13:21:51.487: E/AndroidRuntime(365):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 13:21:51.487: E/AndroidRuntime(365):  at dalvik.system.NativeStart.main(Native Method)
01-09 13:21:51.487: E/AndroidRuntime(365): Caused by: java.lang.reflect.InvocationTargetException
01-09 13:21:51.487: E/AndroidRuntime(365):  at java.lang.reflect.Constructor.constructNative(Native Method)
01-09 13:21:51.487: E/AndroidRuntime(365):  at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.view.LayoutInflater.createView(LayoutInflater.java:505)
01-09 13:21:51.487: E/AndroidRuntime(365):  ... 24 more
01-09 13:21:51.487: E/AndroidRuntime(365): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.content.res.Resources.loadDrawable(Resources.java:1709)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.view.View.<init>(View.java:1951)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.view.View.<init>(View.java:1899)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.view.ViewGroup.<init>(ViewGroup.java:286)
01-09 13:21:51.487: E/AndroidRuntime(365):  at android.widget.RelativeLayout.<init>(RelativeLayout.java:173)
01-09 13:21:51.487: E/AndroidRuntime(365):  ... 27 more
01-09 13:21:51.517: W/ActivityManager(60):   Force finishing activity com.umidtech.tabbacwardcompatible/.MainActivity
01-09 13:21:52.036: W/ActivityManager(60): Activity pause timeout for HistoryRecord{407200a8                com.umidtech.tabbacwardcompatible/.MainActivity}
01-09 13:21:53.906: I/Process(365): Sending signal. PID: 365 SIG: 9
01-09 13:21:53.927: I/ActivityManager(60): Process com.umidtech.tabbacwardcompatible (pid 365) has died.
01-09 13:21:53.927: I/WindowManager(60): WIN DEATH: Window{406e6ca8 com.umidtech.tabbacwardcompatible/com.umidtech.tabbacwardcompatible.MainActivity paused=false}
01-09 13:21:54.026: W/InputManagerService(60): Got RemoteException sending setActive(false) notification to pid 365 uid 10039
01-09 13:22:02.586: W/ActivityManager(60): Activity destroy timeout for HistoryRecord{407200a8 com.umidtech.tabbacwardcompatible/.MainActivity}

0 个答案:

没有答案