使用ViewPager和Fragments在Android TabLayout中运行时错误

时间:2016-07-27 07:58:36

标签: android android-studio android-fragments

我使用ViewPager和Fragments在Android中创建了一个TabLayout应用程序。我成功构建了应用程序。但是在将apk安装到设备后,我的应用程序很快停止了。请帮我解决问题。

MainActiviti.java

  package com.asoft.admin.anto_tab;  
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {

//This is our tablayout
private TabLayout tabLayout;

//This is our viewPager
private ViewPager viewPager;

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


    //Adding toolbar to the activity
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //Initializing the tablayout
    tabLayout = (TabLayout) findViewById(R.id.tabLayout);

    //Adding the tabs using addTab() method
    tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    //Initializing viewPager
    viewPager = (ViewPager) findViewById(R.id.pager);

    //Creating our pager adapter
    Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());

    //Adding adapter to pager
    viewPager.setAdapter(adapter);

    //Adding onTabSelectedListener to swipe views
    tabLayout.setOnTabSelectedListener(this);


}

@Override
public void onTabSelected(TabLayout.Tab tab) {

}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
}

Pager.java

package com.asoft.admin.anto_tab;
/**
 * Created by Admin on 27-07-2016.
 */
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Belal on 2/3/2016.
*/

//Our class extending fragment
public class Tab1 extends Fragment {

//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //Returning the layout file after inflating
    //Change R.layout.tab1 in you classes
    return inflater.inflate(R.layout.tab1, container, false);
}
}

Tab1.java

package com.asoft.admin.anto_tab;
/**
 * Created by Admin on 27-07-2016.
 */
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Belal on 2/3/2016.
*/

//Our class extending fragment
public class Tab1 extends Fragment {

//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //Returning the layout file after inflating
    //Change R.layout.tab1 in you classes
    return inflater.inflate(R.layout.tab1, container, false);
}
}

Tab2.java

 package com.asoft.admin.anto_tab;

/**
 * Created by Admin on 27-07-2016.
 */
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Belal on 2/3/2016.
 */

//Our class extending fragment
public class Tab2 extends Fragment {

//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //Returning the layout file after inflating
    //Change R.layout.tab1 in you classes
    return inflater.inflate(R.layout.tab1, container, false);
}
}

Tab3.java

 package com.asoft.admin.anto_tab;

/**
 * Created by Admin on 27-07-2016.
*/
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Belal on 2/3/2016.
 */

//Our class extending fragment
public class Tab3 extends Fragment {

//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //Returning the layout file after inflating
    //Change R.layout.tab1 in you classes
    return inflater.inflate(R.layout.tab1, container, false);
}
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">


<!-- our toolbar -->
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<!-- our tablayout to display tabs  -->
<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"/>


</LinearLayout>

logcat的

07-27 12:21:46.909 2529-2529/com.asoft.admin.anto_tab I/art: Not late- enabling -Xcheck:jni (already on)
07-27 12:21:47.080 2529-2529/com.asoft.admin.anto_tab W/System: ClassLoader  referenced unknown path: /data/app/com.asoft.admin.anto_tab-1/lib/x86
07-27 12:22:01.740 2529-2529/com.asoft.admin.anto_tab W/System: ClassLoader referenced unknown path: /data/app/com.asoft.admin.anto_tab-1/lib/x86
07-27 12:22:01.988 2529-2529/com.asoft.admin.anto_tab 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-27 12:22:02.124 2529-2529/com.asoft.admin.anto_tab D/AndroidRuntime:  Shutting down VM


                                                                    --------- beginning of crash
07-27 12:22:02.124 2529-2529/com.asoft.admin.anto_tab E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process:  com.asoft.admin.anto_tab, PID: 2529
                                                                     java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.asoft.admin.anto_tab/com.asoft.admin.anto_tab.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(AppCompatDele gateImplV7.java:198)
                                                                        at  android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
                                                                        at com.asoft.admin.anto_tab.MainActivity.onCreate(MainActivity.java:25)
                                                                        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-27 12:22:07.113 2529-2535/com.asoft.admin.anto_tab W/art: Suspending all  threads took: 5.201ms
07-27 12:22:22.184 2529-2535/com.asoft.admin.anto_tab W/art: Suspending all threads took: 19.789ms
07-27 12:22:22.350 2529-2529/com.asoft.admin.anto_tab I/Process: Sending  signal.    PID: 2529 SIG: 9

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

1 个答案:

答案 0 :(得分:0)

在style.xml <intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" /> 中更改您的父主题。因为错误说您的主题提供了一个操作,而您正在添加xml。 有关更多信息,请参阅: - http://coderzpassion.com/android-working-with-material-design/