2015-09-14 136 views
0

我是新來的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清單

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

你的三個'Fragments'擴展了android.support.v4.app.Fragment或者android.app.Fragment嗎? – akodiakson

+0

您嘗試使用Android Studio提供的模板設置您的新項目。步驟:文件 - >新建 - >新建項目。然後輸入你的項目名稱和其他細節。點擊next,然後在Add Activity to mobile選擇'Tabbed Activity',點擊next並選擇'Navigation Style'中的'Action Bar Tabs(with View Pager)'。 –

+0

不,它只擴展片段 –

回答

0

BriefFragmentIntroFragmentStaffFragment需要延長android.support.v4.app.Fragment代替android.app.Fragment。如果您不確定您正在使用哪一個,請檢查您的導入。

+0

這是正確的,另一種選擇是使用v13支持庫中的FragmentStatePagerAdapter,它允許使用常規片段。 –

+0

是啊你的答案工作,我現在沒有任何錯誤,但不幸的是,當我運行該應用程序,它只是崩潰。請任何方式? –

+0

如果解決了問題,您可以接受答案,然後將有關新崩潰的詳細信息發佈爲修改或新問題。 – akodiakson