2016-03-26 154 views
-2

我寫的代碼來實現與小部件可滾動的標籤,但不幸的是,我有一個問題,當我啓動應用程序: Android的工作室指向這一行:滾動標籤

setSupportActionBar(工具欄);

如果有人可以幫助我。

這是我的活動代碼:

import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 

public class HomePage extends AppCompatActivity { 

    Intent intent; 

    private Toolbar toolbar; 
    private TabLayout tabLayout; 
    private ViewPager viewPager; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_homepage); 

     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

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

     tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(viewPager); 
     Bundle extras =getIntent().getExtras(); 
     if(extras!=null) 
      Toast.makeText(getApplicationContext(), extras.getString("username").toString(), Toast.LENGTH_LONG).show();; 


    } 

    private void setupViewPager(ViewPager viewPager) { 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
     adapter.addFrag(new OneFragment(), "ONE"); 
     adapter.addFrag(new TwoFragment(), "TWO"); 
     adapter.addFrag(new ThreeFragment(), "THREE"); 
     adapter.addFrag(new FourFragment(), "FOUR"); 
     adapter.addFrag(new FiveFragment(), "FIVE"); 
     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 addFrag(Fragment fragment, String title) { 
      mFragmentList.add(fragment); 
      mFragmentTitleList.add(title); 
     } 

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

這是佈局的xml文件:

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabMode="scrollable"/> 
    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
</android.support.design.widget.CoordinatorLayout> 

這是logcat的:

3-26 07:01:40.136 5424-5424/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4d49b20) 
03-26 07:01:40.136 5424-5424/? E/AndroidRuntime: FATAL EXCEPTION: main 
               Process: com.subhi.hccc, PID: 5424 
               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.subhi.hccc/com.subhi.hccc.HomePage}: 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:2184) 
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                at android.os.Handler.dispatchMessage(Handler.java:102) 
                at android.os.Looper.loop(Looper.java:136) 
                at android.app.ActivityThread.main(ActivityThread.java:5001) 
                at java.lang.reflect.Method.invokeNative(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:515) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                at dalvik.system.NativeStart.main(Native Method) 
                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.subhi.hccc.HomePage.onCreate(HomePage.java:32) 
                at android.app.Activity.performCreate(Activity.java:5231) 
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)  
                at android.app.ActivityThread.access$800(ActivityThread.java:135)  
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  
                at android.os.Handler.dispatchMessage(Handler.java:102)  
                at android.os.Looper.loop(Looper.java:136)  
                at android.app.ActivityThread.main(ActivityThread.java:5001)  
                at java.lang.reflect.Method.invokeNative(Native Method)  
                at java.lang.reflect.Method.invoke(Method.java:515)  
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)  
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)  
                at dalvik.system.NativeStart.main(Native Method)  
03-26 07:01:40.160 523-816/? W/ActivityManager: Force finishing activity com.subhi.hccc/.HomePage 
03-26 07:01:40.552 523-816/? D/dalvikvm: GC_FOR_ALLOC freed 1011K, 27% free 8922K/12140K, paused 8ms, total 8ms 
03-26 07:01:40.576 523-816/? D/dalvikvm: GC_FOR_ALLOC freed 392K, 27% free 8902K/12140K, paused 12ms, total 13ms 
03-26 07:01:40.576 523-816/? W/ActivityManager: Force finishing activity info.androidhive.materialtabs/.activity.ScrollableTabsActivity 
03-26 07:01:40.616 523-538/? D/dalvikvm: GC_FOR_ALLOC freed 12K, 23% free 9384K/12140K, paused 7ms, total 7ms 
03-26 07:01:40.616 523-538/? I/dalvikvm-heap: Grow heap (frag case) to 10.295MB for 1127532-byte allocation 
03-26 07:01:40.636 523-538/? D/dalvikvm: GC_FOR_ALLOC freed <1K, 21% free 10484K/13244K, paused 7ms, total 7ms 
03-26 07:01:40.652 523-567/? D/MobileDataStateTracker: default: setPolicyDataEnable(enabled=true) 
03-26 07:01:40.900 150-150/? W/SurfaceFlinger: couldn't log to binary event log: overflow. 
03-26 07:01:41.076 523-538/? W/ActivityManager: Activity pause timeout for ActivityRecord{52934b38 u0 com.subhi.hccc/.HomePage t62 f} 
03-26 07:01:41.176 4763-4763/? W/EGL_genymotion: eglSurfaceAttrib not implemented 
03-26 07:01:41.936 523-567/? D/MobileDataStateTracker: default: setPolicyDataEnable(enabled=true) 
03-26 07:01:42.072 523-567/? D/MobileDataStateTracker: default: setPolicyDataEnable(enabled=true) 
03-26 07:01:43.448 5424-5424/? I/Process: Sending signal. PID: 5424 SIG: 9 
03-26 07:01:43.452 523-567/? D/MobileDataStateTracker: default: setPolicyDataEnable(enabled=true) 
03-26 07:01:43.452 153-503/? W/genymotion_audio: out_write() limiting sleep time 46802 to 39909 
03-26 07:01:43.480 523-753/? I/ActivityManager: Process com.subhi.hccc (pid 5424) has died. 
+0

什麼錯誤/警告是,當你把你的光標移動到那個地方出?或者你有任何錯誤日誌,這將有助於確定問題。 –

+0

檢查更新plesas –

+0

把你的風格XML文件,我認爲你沒有采取適當的風格在你的XML這就是爲什麼它給你錯誤。 –

回答

0

確保您的主旋律清單中的樣式不會擴展那些DarkActionBar樣式之一。 請確保它從Theme.AppCompat.Light擴展。 NoActionBar(例如)。否則在已經有工具欄的應用程序中添加工具欄。

並會提出你在logcat上看到的錯誤。

編碼快樂:)

UPDATE

這是怎麼看起來像你的清單

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/**AppTheme**"> 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.**NoActionBar**"> 
    </activity> 
</application> 
+0

這是完全錯誤的想法;這與黑暗行動欄主題無關;實際上應用程序主題必須有一個黑暗的動作欄主題才能顯示自定義工具欄; @Sergio Lima – DJphy

+0

請記住,在上述snipet代碼中,他將一個自定義工具欄添加到活動中。如果清單中的主題已經使用了一個操作欄的主題,則最終會有兩個操作欄。這就是爲什麼你會犯這個錯誤。請確保在聲明清單上的活動時,您將主題指定爲否則它將從應用程序主題擴展有一個操作欄 –