2016-07-02 64 views
1

我不知道什么是错误的项目 这是主要活动文件 即时learnin如何使用选项卡视图这我已经尝试了几次应用程序崩溃开始我已附加日志猫和主要活动代码,请帮我出应用程序在Android中使用选项卡视图崩溃

package com.example.harsimar.tabswithswipableviews; 

import android.app.ActionBar; 
import android.app.FragmentTransaction; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 

public class MainActivity extends AppCompatActivity { 
    private String[] tabNames = {"First", "Second", "Third"}; 
    Toolbar toolbar; 
    TabLayout tabLayout ; 
    ViewPager viewPager; 
    TagPagerAdapter tagPagerAdapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     toolbar=(Toolbar)findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     tabLayout=(TabLayout)findViewById(R.id.tablayout); 
     viewPager=(ViewPager)findViewById(R.id.viewPager); 
     tagPagerAdapter=new TagPagerAdapter(getSupportFragmentManager()); 
     tagPagerAdapter.addFragments(new FirstTab(),"fragment tab 1"); 
     tagPagerAdapter.addFragments(new SecondTab(),"fragment tab 2"); 
     tagPagerAdapter.addFragments(new ThirdTab(),"fragment tab 3"); 
     viewPager.setAdapter(tagPagerAdapter); 
     tabLayout.setupWithViewPager(viewPager); 

    } 
} 

And this is the TagPagerAdapter class file 



package com.example.harsimar.tabswithswipableviews; 

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 

import java.util.ArrayList; 

/** 
* Created by Harsimar on 02-07-2016. 
*/ 
public class TagPagerAdapter extends FragmentPagerAdapter { 
    ArrayList<Fragment> fragments=new ArrayList<>(); 
    ArrayList<String> tabTitles =new ArrayList<>(); 

    public void addFragments(Fragment fragments,String titles){ 
     this.fragments.add(fragments); 
     this.tabTitles.add(titles); 

    } 

    public TagPagerAdapter(FragmentManager fm){ 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return fragments.get(position); 
    } 

    @Override 
    public int getCount() { 
     return fragments.size(); 
    } 

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


The main layout xml looks like this 



<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:context="com.example.harsimar.tabswithswipableviews.MainActivity"> 
    <android.support.design.widget.AppBarLayout 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     > 
     <include 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      layout="@layout/toolbar_layout" 
      /> 
     <android.support.design.widget.TabLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/tablayout" 
      app:tabMode="fixed" 
      app:tabGravity="fill"> 
     </android.support.design.widget.TabLayout> 
     <android.support.v4.view.ViewPager 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/viewPager"> 
     </android.support.v4.view.ViewPager> 
    </android.support.design.widget.AppBarLayout> 

</RelativeLayout> 

this is just a basic app to test and learn tab views 
but app just crashes before starting 
log cat shows 


07-02 23:08:11.604 22298-22298/com.example.harsimar.tabswithswipableviews I/art: Not late-enabling -Xcheck:jni (already on) 
07-02 23:08:12.103 22298-22298/com.example.harsimar.tabswithswipableviews W/System: ClassLoader referenced unknown path: /data/app/com.example.harsimar.tabswithswipableviews-2/lib/x86 
07-02 23:08:14.171 22298-22304/com.example.harsimar.tabswithswipableviews W/art: Suspending all threads took: 7.484ms 
07-02 23:08:15.678 22298-22304/com.example.harsimar.tabswithswipableviews W/art: Suspending all threads took: 8.872ms 
07-02 23:08:23.459 22298-22298/com.example.harsimar.tabswithswipableviews W/System: ClassLoader referenced unknown path: /data/app/com.example.harsimar.tabswithswipableviews-2/lib/x86 
07-02 23:08:23.791 22298-22304/com.example.harsimar.tabswithswipableviews W/art: Suspending all threads took: 6.128ms 
07-02 23:08:24.038 22298-22298/com.example.harsimar.tabswithswipableviews W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
07-02 23:08:24.460 22298-22298/com.example.harsimar.tabswithswipableviews D/AndroidRuntime: Shutting down VM 
07-02 23:08:24.463 22298-22298/com.example.harsimar.tabswithswipableviews E/AndroidRuntime: FATAL EXCEPTION: main 
                          Process: com.example.harsimar.tabswithswipableviews, PID: 22298 
                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.harsimar.tabswithswipableviews/com.example.harsimar.tabswithswipableviews.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:2416) 
                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                           at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                           at android.os.Looper.loop(Looper.java:148) 
                           at android.app.ActivityThread.main(ActivityThread.java:5417) 
                           at java.lang.reflect.Method.invoke(Native Method) 
                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                          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:126) 
                           at com.example.harsimar.tabswithswipableviews.MainActivity.onCreate(MainActivity.java:23) 
                           at android.app.Activity.performCreate(Activity.java:6237) 
                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
                           at android.app.ActivityThread.-wrap11(ActivityThread.java)  
                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
                           at android.os.Handler.dispatchMessage(Handler.java:102)  
                           at android.os.Looper.loop(Looper.java:148)  
                           at android.app.ActivityThread.main(ActivityThread.java:5417)  
                           at java.lang.reflect.Method.invoke(Native Method)  
                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
07-02 23:09:02.204 22298-22298/com.example.harsimar.tabswithswipableviews I/Process: Sending signal. PID: 22298 SIG: 9 
+0

检查http://stackoverflow.com/q/26515058/2435238 – sJy

回答

0

styles.xml

<item name="windowActionBar">false</item> 
<item name="windowNoTitle">true</item> 

这些行添加到您的活动的主题,例如:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 

,然后在你的AndroidManifest.xml 一套这个主题的活动

<activity 
     android:name=".MainActivity" 
     android:theme="@style/AppTheme"> 
</activity>