简体   繁体   中英

Android swipe tab crash

I am new to android and developing my first application as my research project for Bachelor of Computer Science. I am have create a swipe tab with three tabs, but when I run the application on an emulator and my device, it just crash please can anybody help me with the solution;

here is the code

Main Class

package com.thenextgeneration.umyucsc;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;

import com.thenextgeneration.umyucsc.MyAdapterOne;
import com.thenextgeneration.umyucsc.IntroFragment;
import com.thenextgeneration.umyucsc.BriefFragment;
import com.thenextgeneration.umyucsc.StaffFragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

public class DepartmentTab extends FragmentActivity implements    TabListener{


ViewPager viewPager;
ActionBar actionBar;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
    setContentView(R.layout.departmenttab);

    viewPager = (ViewPager) findViewById(R.id.departmentPager);
    viewPager.setAdapter(new MyAdapterOne(getSupportFragmentManager()));
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            actionBar.setSelectedNavigationItem(arg0);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    });

    actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab introduction = actionBar.newTab();
    introduction.setText("INTRODUCTION");
    introduction.setTabListener(this);

    ActionBar.Tab brief = actionBar.newTab();
    brief.setText("BRIEF HISTORY");
    brief.setTabListener(this);

    ActionBar.Tab staff = actionBar.newTab();
    staff.setText("STAFF LIST");
    staff.setTabListener(this);

    actionBar.addTab(introduction);
    actionBar.addTab(brief);
    actionBar.addTab(staff);
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    viewPager.setCurrentItem(tab.getPosition());

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

}

class MyAdapterOne extends FragmentPagerAdapter {

public MyAdapterOne(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int arg0) {
    // TODO Auto-generated method stub
    Fragment fragment = null;
    if(arg0 == 0) {
        fragment = new IntroFragment();
    }
    if(arg0 == 1) {
        fragment = new BriefFragment();
    }
    if(arg0 == 2) {
        fragment = new StaffFragment();
    }
    return fragment;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 3;
}

}

sample fragment

package com.thenextgeneration.umyucsc;

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 *
 */
public class IntroFragment extends android.support.v4.app.Fragment {

public IntroFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_intro, container, false);
}

}

Sample xml layout

<FrameLayout 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:background="#FFFFFF"
tools:context="com.thenextgeneration.umyucsc.IntroFragment" >

<!-- TODO: Update blank fragment layout -->

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
         >

         <ImageView
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:src="@drawable/intro" />

        </RelativeLayout>
    </ScrollView>

</FrameLayout>

main class xml layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.View.ViewPager 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.thenextgeneration.DepartmentTab"
android:id="@+id/departmentPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
/>

android manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thenextgeneration.umyucsc"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".DepartmentTab"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

BriefFragment , IntroFragment and StaffFragment need to extend android.support.v4.app.Fragment instead of android.app.Fragment . Check your imports if you aren't sure which one you are using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM