2016-03-26 23 views
-2

i einen Code geschrieben, um eine scrollbare Registerkarten mit Widgets zu implementieren, aber leider habe ich ein Problem, wenn ich die Anwendung zu starten: das Android-Studio zu dieser Linie zeigt:Scrollbare Tabs in android Fehler

setSupportActionBar (Toolbar);

wenn mir bitte jemand helfen kann.

Das ist mein Aktivitätscode:

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); 
     } 
    } 
} 

Und das ist das Layout XML-Datei:

<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> 

Und das ist die 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

, was den Fehler/Warnung es zeigt, wenn Sie an dieser Stelle den Cursor nehmen? Oder ein Fehlerprotokoll, das Sie haben, dann wird es hilfreich sein, das Problem zu identifizieren. –

+0

Überprüfen Sie die Updates plesas –

+0

Legen Sie Ihre Stil XML-Datei, ich denke, Sie haben nicht richtig Stil in Ihrem XML, das ist, warum es Ihnen Fehler gibt. –

Antwort

0

Ihr Hauptthema stellen Sie sicher, Stil im Manifest erweitert keinen dieser DarkActionBar-Stile. stellen Sie bitte sicher, dass es sich von einer Theme.AppCompat.Light erstreckt. NoActionBar (zum Beispiel). Andernfalls fügen Sie eine Symbolleiste zu einer App hinzu, die bereits über eine Symbolleiste verfügt.

Und wird diesen Fehler, den Sie auf dem Logcat sehen.

glücklich Codierung :)

UPDATE

Dies ist, wie es aussehen auf Ihrem Manifest

<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

das ist völlig falsch Idee; es hat nichts mit dem dunklen Action-Bar-Thema zu tun; Tatsächlich muss das App-Design ein dunkles Aktionsleisten-Design aufweisen, um eine benutzerdefinierte Symbolleiste anzuzeigen. @Sergio Lima – DJphy

+0

Bedenken Sie, dass auf dem obigen Code-Snipet eine benutzerdefinierte Symbolleiste zu einer Aktivität hinzugefügt wird. Wenn das Thema im Manifest bereits ein Thema mit einer Aktionsleiste verwendet, erhalten Sie zwei Aktionsleisten. Deshalb bekommst du diesen Fehler. stellen Sie sicher, dass Sie bei der Deklaration der Aktivität auf dem Manifest das Thema als angeben, andernfalls wird es vom Anwendungsthema ausgehen, das bereits könnte habe eine Aktionsleiste –