gpt4 book ai didi

java - 为什么我会收到此错误?请查看详情

转载 作者:行者123 更新时间:2023-12-02 04:19:49 25 4
gpt4 key购买 nike

我正在开发一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com