2015-09-07 41 views
1

崩潰,我試圖按照下面的教程來創建標籤視圖佈局:http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.htmlAndroid應用程序在setSupportActionBar

不幸的是,應用程序崩潰保持任何事情之前走了過來。將調試日誌插入我的代碼後,我發現問題存在於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中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity"> 

    <include 
     android:id="@+id/tool_bar" 
     layout="@layout/tool_bar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     /> 

    <com.example.rkhaj.tabstest.SlidingTabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:elevation="2dp" 
     android:background="@color/ColorPrimary"/> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     > 
    </android.support.v4.view.ViewPager> 

</LinearLayout> 

這是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 
+0

給我們的activity_main xml和logcat的。 – poss

+0

如果發生崩潰,則會出現堆棧跟蹤。展示下。 –

回答

5

由於您在佈局中使用Toolbar,因此應該使用Theme.AppCompat.NoActionBar作爲您的父主題。

否則,你可以使用somenthing像:

<style name="MyTheme" parent="Theme.AppCompat"> 
    ... 
</style> 

<style name="MyTheme.NoActionBar"> 
    <!-- Both of these are needed --> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 
+0

該解決方案可節省大量時間。 Android沒有提供任何有用的調試信息。 –

0

您需要使用的程序兼容性主題活動。像這樣的一個,Theme.AppCompat.Light.NoActionBar

+0

我目前使用的主題是Theme.AppCompat.Light.DarkActionBar –

+0

您需要使用NoActionBar,或者我認爲至少「 false true