0

嗨我正在做这个应用程序与两个片段,你可以滑动之间。该代码编译并运行时不会崩溃,并显示两个选项卡,您可以在选项卡之间滑动,但选项卡没有内容。我认为这是试图设置内容视图的片段的问题。如果您需要了信息只是问:)片段标签内容与HoloEverywhere和ActionbarSherlock不显示

我的主要活动

import java.util.ArrayList; 

import org.holoeverywhere.app.Activity; 
import org.holoeverywhere.widget.TextView; 
import com.actionbarsherlock.app.ActionBar; 
import com.actionbarsherlock.app.ActionBar.Tab; 
import com.actionbarsherlock.app.SherlockActivity; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.view.ViewPager; 
import android.view.WindowManager; 

public class SwipeTabsMainActivity extends Activity { 
ViewPager mViewPager; 
TabsAdapter mTabsAdapter; 
TextView tabCenter; 
TextView tabText; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    mViewPager = new ViewPager(this); 
    mViewPager.setId(R.id.pager); 

    setContentView(mViewPager); 
    ActionBar bar = getSupportActionBar(); 
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); 

    mTabsAdapter = new TabsAdapter(this, mViewPager); 

     mTabsAdapter.addTab(bar.newTab().setText("Stopwatch"),StopWatchFragment.class, null); 
    mTabsAdapter.addTab(bar.newTab().setText("Timer"), CountdownFragment.class, null); 



} 

public static class TabsAdapter extends FragmentPagerAdapter implements 
     ActionBar.TabListener, ViewPager.OnPageChangeListener { 
    private final Context mContext; 
    private final ActionBar mActionBar; 
    private final ViewPager mViewPager; 
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>(); 

    static final class TabInfo { 
     private final Class<?> clss; 
     private final Bundle args; 

     TabInfo(Class<?> _class, Bundle _args) { 
      clss = _class; 
      args = _args; 
     } 
    } 

    public TabsAdapter(Activity activity, ViewPager pager) { 
     super(activity.getSupportFragmentManager()); 
     mContext = activity; 
     mActionBar = activity.getSupportActionBar(); 
     mViewPager = pager; 
     mViewPager.setAdapter(this); 
     mViewPager.setOnPageChangeListener(this); 
    } 

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) { 
     TabInfo info = new TabInfo(clss, args); 
     tab.setTag(info); 
     tab.setTabListener(this); 
     mTabs.add(info); 
     mActionBar.addTab(tab); 
     notifyDataSetChanged(); 
    } 

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

    @Override 
    public Fragment getItem(int position) { 
     TabInfo info = mTabs.get(position); 
     return Fragment.instantiate(mContext, info.clss.getName(), 
       info.args); 
    } 

    @Override 
    public void onPageScrolled(int position, float positionOffset, 
      int positionOffsetPixels) { 
    } 

    @Override 
    public void onPageSelected(int position) { 
     mActionBar.setSelectedNavigationItem(position); 
    } 

    @Override 
    public void onPageScrollStateChanged(int state) { 
    } 

    @Override 
    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     Object tag = tab.getTag(); 
     for (int i = 0; i < mTabs.size(); i++) { 
      if (mTabs.get(i) == tag) { 
       mViewPager.setCurrentItem(i); 
      } 

     } 
    } 

    @Override 
    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
    } 

    @Override 
    public void onTabReselected(Tab tab, FragmentTransaction ft) { 
    } 
} 
} 

主要活动的XML

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/pager" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" /> 

我的倒计时片段

import org.holoeverywhere.app.Fragment; 
import org.holoeverywhere.internal._View; 
import org.holoeverywhere.preference.PreferenceManager; 
import org.holoeverywhere.preference.SharedPreferences; 
import org.holoeverywhere.widget.Button; 
import org.holoeverywhere.widget.TextView; 
import org.holoeverywhere.widget.Toast; 

import android.content.Context; 
import android.os.Bundle; 
import android.os.Vibrator; 
import org.holoeverywhere.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Chronometer; 
import android.widget.ImageButton; 
import android.widget.Chronometer.OnChronometerTickListener; 
import android.widget.RelativeLayout; 

public class CountdownFragment extends Fragment implements 
    OnChronometerTickListener { 
TextView second, hour, minute; 
ImageButton hourup, hourdown, minuteup, minutedown, secondup, seconddown; 
Button start, stop; 
int hourCount, minCount, secCount; 
Context c; 
Chronometer count; 
boolean viberate, running; 
Vibrator v; 
boolean startstopshow; 
SharedPreferences getPrefs; 
View fv = getView(); 
RelativeLayout rl; 
@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    initilize(c); 
    setClicker(); 
    count.start(); 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setRetainInstance(true); 
    c = getActivity(); 




} 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.countdown, container, false); 
    rl = (RelativeLayout)inflater.inflate(R.layout.countdown, container, false); 

    return super.onCreateView(inflater, container, savedInstanceState); 
} 



private void initilize(Context c) { 
    hourup = (ImageButton) rl.findViewById(R.id.hourup); 
    minuteup = (ImageButton) rl.findViewById(R.id.minuteup); 
    secondup = (ImageButton) rl.findViewById(R.id.secondup); 
    hourdown = (ImageButton) rl.findViewById(R.id.hourdown); 
    minutedown = (ImageButton) rl.findViewById(R.id.minutedown); 
    seconddown = (ImageButton) rl.findViewById(R.id.seconddown); 
    start = (Button) rl.findViewById(R.id.start); 
    stop = (Button) rl.findViewById(R.id.stop); 
    second = (TextView) rl.findViewById(R.id.second); 
    minute = (TextView) rl.findViewById(R.id.minute); 
    hour = (TextView) rl.findViewById(R.id.hour); 
    hourCount = 0; 
    minCount = 0; 
    secCount = 0; 

    count = (Chronometer) rl.findViewById(R.id.chronometer1); 
    v = (Vibrator) c.getSystemService(Context.VIBRATOR_SERVICE); 
    running = false; 
    getPrefs = PreferenceManager.getDefaultSharedPreferences(c); 
} 

@Override 
public void onChronometerTick(Chronometer chronometer) { 
    switch (chronometer.getId()) { 

    case (R.id.chronometer1): 
     secCount--; 
     updateDisplay(); 
     break; 
    } 
} 

} 

我的片段xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<TableLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="56dp" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <ImageButton 
      android:id="@+id/hourup" 
      style="?borderlessButtonStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/up" /> 

     <ImageButton 
      android:id="@+id/minuteup" 
      style="?borderlessButtonStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/up" /> 

     <ImageButton 
      android:id="@+id/secondup" 
      style="?borderlessButtonStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/up" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <org.holoeverywhere.widget.TextView 
      android:id="@+id/hour" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="00 : " 
      android:textSize="35sp" /> 

     <org.holoeverywhere.widget.TextView 
      android:id="@+id/minute" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="00 : " 
      android:textSize="35sp" /> 

     <org.holoeverywhere.widget.TextView 
      android:id="@+id/second" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="00" 
      android:textSize="35sp" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <ImageButton 
      android:id="@+id/hourdown" 
      style="?borderlessButtonStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/down" /> 

     <ImageButton 
      android:id="@+id/minutedown" 
      style="?borderlessButtonStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/down" /> 

     <ImageButton 
      android:id="@+id/seconddown" 
      style="?borderlessButtonStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/down" /> 
    </TableRow> 
</TableLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_weight="50" 
    android:gravity="bottom" 
    android:weightSum="100" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="48dp" 
     android:layout_alignParentBottom="true" > 

     <org.holoeverywhere.internal._View 
      android:id="@+id/view1" 
      android:layout_width="match_parent" 
      android:layout_height="1dip" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="4dip" 
      android:layout_marginRight="4dip" 
      android:background="?dividerVertical" /> 

     <org.holoeverywhere.internal._View 
      android:id="@+id/ViewColorPickerHelper" 
      android:layout_width="1dip" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginBottom="4dip" 
      android:layout_marginTop="4dip" 
      android:background="?dividerVertical" /> 

     <org.holoeverywhere.widget.Button 
      android:id="@+id/start" 
      style="@style/Holo.Button.Borderless" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/view1" 
      android:layout_alignParentTop="true" 
      android:layout_toLeftOf="@+id/stop" 
      android:text="Start" /> 

     <org.holoeverywhere.widget.Button 
      android:id="@+id/stop" 
      style="@style/Holo.Button.Borderless" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" 
      android:layout_toRightOf="@+id/ViewColorPickerHelper" 
      android:text="Stop" /> 
    </RelativeLayout> 
</LinearLayout> 

<Chronometer 
    android:id="@+id/chronometer1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="Chronometer" 
    android:textSize="0px" 
    android:visibility="invisible" /> 

</RelativeLayout> 

screenshot

+1

可以粘贴一些屏幕截图? – SKK

回答

2

我终于发现了问题:

必须更换:super.onCreateView(吹气,容器,savedInstanceState);你的倒计时观点:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.countdown, container, false); 
    rl = (RelativeLayout)inflater.inflate(R.layout.countdown, container, false); 

    return v; 
} 

这里的结果的截图:

enter image description here

+0

谢谢你解决了这个问题。 – user1762507

0

我可以看到你正在使用的ActionBarSherlock。试试这个库:http://viewpagerindicator.com/。它会帮助你,它很容易定制你的标签和导航

你可以看到谷歌play上的应用程序的例子很多:https://play.google.com/store/apps/details?id=com.viewpagerindicator.sample&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS52aWV3cGFnZXJpbmRpY2F0b3Iuc2FtcGxlIl0

下面是一个例子: 主要活动:

import android.os.Bundle; 
import android.support.v4.view.ViewPager; 


import com.actionbarsherlock.app.SherlockFragmentActivity; 
import com.viewpagerindicator.TabPageIndicator; 
public class MainActivity extends SherlockFragmentActivity { 

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

     ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager); 


     mViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager())); 

      //Bind the title indicator to the adapter 
     TabPageIndicator tabIndicator = (TabPageIndicator)findViewById(R.id.titles); 
     tabIndicator.setViewPager(mViewPager); 


     mViewPager.setCurrentItem(1); 
    } 


} 

FragmentPagerAdapter:

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

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{ 
    private Fragment[] fragments; 
    private static final String[] CONTENT = new String[] { "frag A","frag B", "frag C" }; 

    // Reference : https://bitbucket.org/owentech/abstabsviewpager/downloads 


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

     this.fragments = new Fragment[] { new FragmentA(), 
       new FragmentB(), 
       new FragmentC()}; 
    } 

    @Override 
    public Fragment getItem(int arg0) { 
     // TODO Auto-generated method stub 
     return this.fragments[arg0]; 


    } 

    public CharSequence getPageTitle(int position) { 
     return CONTENT[position]; 
    } 

    @Override 
    public int getCount() { 

     return this.fragments.length; 

    } 


} 

FragmentX:

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


public class FragmentA extends Fragment{ 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View v = inflater.inflate(R.layout.fragmenta, container, false); 


     return v; 


    } 

} 

主要XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <com.viewpagerindicator.TabPageIndicator 
    android:id="@+id/titles" 
    android:theme="@style/Theme.PageIndicatorDefaults" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent"/> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 
</LinearLayout> 

清单XML:

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

    <uses-sdk 
     android:minSdkVersion="10" 
     android:targetSdkVersion="15" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/Theme.Sherlock" > 
     <activity 
      android:name="com.example.viewpagertest.MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/Theme.PageIndicatorDefaults" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

</manifest> 
+0

使用该库无助于我认为问题可能与片段本身有关。 – user1762507

+0

我明白,但实现带有该库的指标的viewpager更容易。我会尝试调试你的代码并发布另一个答案来帮助你 – Frank