Android应用程序在setSupportActionBar崩溃

我正在尝试按照以下教程创建选项卡视图布局: http : //www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html

不幸的是,应用程序在出现任何问题之前都会崩溃。 将调试日志插入我的代码后,我发现问题存在于MainActivity中的setSupportActionBar(toolbar)中。 我不知道为什么会这样。 我已粘贴下面的整个MainActivity文件。

 package com.example.rkhaj.tabstest; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { // Declaring Your View and Variables Toolbar toolbar; ViewPager pager; ViewPagerAdapter adapter; SlidingTabLayout tabs; CharSequence Titles[]={"Home","Events"}; int Numboftabs =2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("icecream", "position 1"); // Creating The Toolbar and setting it as the Toolbar for the activity toolbar = (Toolbar) findViewById(R.id.tool_bar); Log.d("icecream", "position 1.5"); setSupportActionBar(toolbar); Log.d("icecream", "position 2"); // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs. adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs); Log.d("icecream", "position 3"); // Assigning ViewPager View and setting the adapter pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(adapter); Log.d("icecream", "position 4"); // Assinging the Sliding Tab Layout View tabs = (SlidingTabLayout) findViewById(R.id.tabs); tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width Log.d("icecream", "position 5"); // Setting Custom Color for the Scroll bar indicator of the Tab View tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { @Override public int getIndicatorColor(int position) { return getResources().getColor(R.color.tabsScrollColor); } }); Log.d("icecream", "position 6"); // Setting the ViewPager For the SlidingTabsLayout tabs.setViewPager(pager); Log.d("icecream", "position 7"); } @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_settings) { return true; } return super.onOptionsItemSelected(item); } } 

这是activity_main.xml:

       

这是logcat:

 09-07 16:46:27.772 22833-22833/com.example.rkhaj.tabstest I/art﹕ Late-enabling -Xcheck:jni 09-07 16:46:27.772 22833-22833/com.example.rkhaj.tabstest I/art﹕ VMHOOK: rlim_cur : 0 pid:22833 09-07 16:46:27.812 22833-22833/com.example.rkhaj.tabstest E/Typeface﹕ SANS_LOC file not found. 09-07 16:46:27.882 22833-22833/com.example.rkhaj.tabstest D/icecream﹕ position 1 09-07 16:46:27.882 22833-22833/com.example.rkhaj.tabstest D/icecream﹕ position 1.5 09-07 16:46:27.882 22833-22833/com.example.rkhaj.tabstest E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.rkhaj.tabstest, PID: 22833 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rkhaj.tabstest/com.example.rkhaj.tabstest.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:155) at android.app.ActivityThread.main(ActivityThread.java:5696) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:197) at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99) at com.example.rkhaj.tabstest.MainActivity.onCreate(MainActivity.java:34) at android.app.Activity.performCreate(Activity.java:5958) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)            at android.app.ActivityThread.access$800(ActivityThread.java:144)            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)            at android.os.Handler.dispatchMessage(Handler.java:102)            at android.os.Looper.loop(Looper.java:155)            at android.app.ActivityThread.main(ActivityThread.java:5696)            at java.lang.reflect.Method.invoke(Native Method)            at java.lang.reflect.Method.invoke(Method.java:372)            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) 09-07 16:46:39.442 22833-22833/com.example.rkhaj.tabstest D/Process﹕ killProcess, pid=22833 09-07 16:46:39.442 22833-22833/com.example.rkhaj.tabstest D/Process﹕ com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:138 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 

由于您在布局中使用Toolbar ,因此应使用Theme.AppCompat.NoActionBar作为父主题。

否则你可以使用somenthing,如:

   

您需要为活动使用AppCompat主题。 像这样,Theme.AppCompat.Light.NoActionBar