ActionBar仅适用于ThemeOverlay

时间:2015-07-31 09:33:34

标签: java android android-actionbar android-theme

我关注如何使用标签制作滑动视图this tutorial。我使用的是Android Studio 1.2.2。

我的所有类都与示例代码中的类相同。当我运行应用程序时,我得到NullPointerException并且应用程序崩溃了。在调试模式下,我发现getActionBar()返回null。

经过一番研究,我发现了两个问题的解决方案:

  1. getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
  2. 之前添加setContentView
  3. Use one Theme.AppCompat个主题
  4. 由于我已经使用了来自Theme.AppCompat的主题,我尝试了第一个解决方案,但它也没有用。然后我开始围绕不同的主题,我发现应用程序正在使用ThemeOverlay中的主题,但如果我正确理解this answer,我不应该使用ThemeOverlay作为一般应用程序主题。

    无论我是否应将ThemeOvarlay用于一般主题,我都希望将其与Theme.AppCompat.Light一起使用。

    经过一些调试后,我发现在Activity.java中的方法initWindowDecorActionBar中,由于某种原因,执行window.getDecorView();之后的第一行是方法中的最后一行。我不知道如何解释这种行为,我所知道的是window.getDecorView();的下一步将我带到方法的最后一行,mActionBar = new WindowDecorActionBar(this);上的断点不会破坏执行。

    private void initWindowDecorActionBar() {
            Window window = getWindow();
    
            // Initializing the window decor can change window feature flags.
            // Make sure that we have the correct set before performing the test below.
            window.getDecorView();
    
            if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
                return;
            }
    
            mActionBar = new WindowDecorActionBar(this);
            mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
    
            mWindow.setDefaultIcon(mActivityInfo.getIconResource());
            mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
        }
    

    有没有人遇到过这样的事情?如何使其与Theme.AppCompat.Light一起使用?

    这是我的档案。

    的build.gradle

    apply plugin: 'com.android.application'
    apply plugin: 'com.neenbedankt.android-apt'
    
    android {
        compileSdkVersion 22
        buildToolsVersion "22.0.1"
    
        defaultConfig {
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            testHandleProfiling true
            testFunctionalTest true
            applicationId "com.test.swipe"
            minSdkVersion 14
            targetSdkVersion 22
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
        packagingOptions {
            exclude 'META-INF/ASL2.0'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/notice.txt'
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:22.2.0'
        ... some other dependencies ...
    }
    

    的AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.mammsoft.phanesmobile" >
    
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
            <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>
    

    MainActivity.java

    package com.test.swipe;
    
    import android.app.ActionBar;
    import android.app.FragmentTransaction;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
    
        AppSectionsPagerAdapter mAppSectionsPagerAdapter;
        ViewPager mViewPager;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
    
            final ActionBar actionBar = getActionBar();
            actionBar.setHomeButtonEnabled(false);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mAppSectionsPagerAdapter);
            mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });
    
            for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
                actionBar.addTab(
                        actionBar.newTab()
                                .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                                .setTabListener(this));
            }
        }
    
        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        }
    
        @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 onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        }
    
        public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
    
            public AppSectionsPagerAdapter(FragmentManager fm) {
                super(fm);
            }
    
            @Override
            public Fragment getItem(int i) {
                switch (i) {
                    case 0:
    
    
                        return new LaunchpadSectionFragment();
    
                    default:
                        Fragment fragment = new DummySectionFragment();
                        Bundle args = new Bundle();
                        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
                        fragment.setArguments(args);
                        return fragment;
                }
            }
    
            @Override
            public int getCount() {
                return 3;
            }
    
            @Override
            public CharSequence getPageTitle(int position) {
                return "Section " + (position + 1);
            }
        }
    
        public static class LaunchpadSectionFragment extends Fragment {
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false);
    
                rootView.findViewById(R.id.demo_collection_button)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(getActivity(), CollectionDemoActivity.class);
                                startActivity(intent);
                            }
                        });
    
                rootView.findViewById(R.id.demo_external_activity)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent externalActivityIntent = new Intent(Intent.ACTION_PICK);
                                externalActivityIntent.setType("image/*");
                                externalActivityIntent.addFlags(
                                        Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                                startActivity(externalActivityIntent);
                            }
                        });
    
                return rootView;
            }
        }
    
        public static class DummySectionFragment extends Fragment {
    
            public static final String ARG_SECTION_NUMBER = "section_number";
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
                Bundle args = getArguments();
                ((TextView) rootView.findViewById(android.R.id.text1)).setText(
                        getString(R.string.dummy_section_text, args.getInt(ARG_SECTION_NUMBER)));
                return rootView;
            }
        }
    }
    

1 个答案:

答案 0 :(得分:1)

更改您的MainActivity以扩展AppCompatActivity,然后使用以下代码获取操作栏:

 final ActionBar actionBar = getSupportActionBar();

重构你的进口,你应该是金色的。