Android滑动标签崩溃

时间:2015-09-14 14:25:48

标签: android

我是android的新手,并开发了我的第一个应用程序作为我的计算机科学学士学位研究项目。我创建了一个带有三个选项卡的滑动选项卡,但是当我在模拟器和我的设备上运行应用程序时,它就会崩溃 请任何人帮我解决问题;

这是代码

主类

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

}

样本片段

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

}

示例xml布局

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

主类xml布局

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

1 个答案:

答案 0 :(得分:0)

BriefFragmentIntroFragmentStaffFragment需要延长android.support.v4.app.Fragment而不是android.app.Fragment。如果您不确定使用哪一种,请检查您的导入。