为什么我收到此错误?请查看详细信息

时间:2015-10-03 07:18:19

标签: java android material-design

我正在开发一个材料设计应用程序&为了应用活动转换,我在MainActivity.java

中编写了以下代码

我的MainActivity.java文件代码:

public class MainActivity extends AppCompatActivity {

    public Toolbar toolbar;
    public TabLayout tabLayout;
    public ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // Call some material design APIs here
            // enable transitions
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        } else {

        }
        setContentView(R.layout.activity_main);

        SpannableString s = new SpannableString("abc");
        s.setSpan(new TypefaceSpan(this, "Pacifico.ttf"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setTitle(s);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        customTabFont();

    }

    private void customTabFont() {

        String fontPath = "fonts/Pacifico.ttf";

        Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabOne.setText("Accept a Request");
        tabOne.setTypeface(tf);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabTwo.setText("Post a Request");
        tabTwo.setTypeface(tf);
        tabLayout.getTabAt(1).setCustomView(tabTwo);

    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new AcceptARequest(), "Accept a Request");
        adapter.addFragment(new PostARequest(), "Post a Request");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_profile) {
            // Check if we're running on Android 5.0 or higher
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // Call some material design APIs here
                getWindow().setExitTransition(new Explode());
                Intent profileIntent = new Intent(MainActivity.this, ProfileActivity.class);
                startActivity(profileIntent, ActivityOptions
                        .makeSceneTransitionAnimation(this).toBundle());
            } else {
                // Implement this feature without material design
                Intent profileIntent = new Intent(MainActivity.this, ProfileActivity.class);
                startActivity(profileIntent);
            }
        } else if (id == R.id.action_settings) {
            Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);
            startActivity(settingsIntent);
        } else if (id == R.id.action_help) {
            Intent helpIntent = new Intent(Intent.ACTION_SEND);
            Intent chooser = Intent.createChooser(helpIntent, "Choose an app");
            helpIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"help@abcxyz123.com"});
            helpIntent.setType("message/rfc822");
            startActivity(chooser);
        } else if (id == R.id.action_faqs) {
            Intent faqsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.abcxyz123.com/faqs"));
            startActivity(faqsIntent);
        } else if (id == R.id.action_about) {
            Intent aboutIntent = new Intent(MainActivity.this, AboutActivity.class);
            startActivity(aboutIntent);
        }

        return super.onOptionsItemSelected(item);
    }
}

运行应用后,我收到以下错误:

android.util.AndroidRuntimeException: requestFeature() must be called before adding content must be called before adding content.

我不明白为什么在添加内容之前添加requestFeature()时出现此错误?

请告诉我。

我是StackOverflow的新手,所以请合作。

提前致谢。

1 个答案:

答案 0 :(得分:2)

您的活动延伸AppCompatActivity,其onCreate()进行了广泛的设置。在致电requestFeature()之前致电super.onCreate()