作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 Material 设计应用程序,为了应用 Activity 转换,我在 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 的新手,所以请配合。
提前致谢。
最佳答案
您的 Activity 扩展了 AppCompatActivity
,后者在其 onCreate()
中进行了大量设置。在调用 super.onCreate()
之前调用 requestFeature()
。
关于java - 为什么我会收到此错误?请查看详情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32919976/
我是一名优秀的程序员,十分优秀!