编辑:我已经重新设计了我的一些code,试图让过去的错误(但现在的应用程序在启动崩溃)。首先,我班现在扩展FragmentActivity这样我就可以访问该方法,getSupportFragmentManager();然后,在上创建,我改变了我的构造函数:
I have reworked some of my code to try and get past the error (but now the app crashes on launch). First, my class now extends FragmentActivity so that I can access the method, getSupportFragmentManager();. Then, within on create, I changed my constructor:
mTabsAdapter = new TabsAdapter(this.getSupportFragmentManager(), this, mViewPager);
从那里,我的子类,我改变了code,以反映这一点:
From there, in my subclass, I changed the code to reflect this:
public TabsAdapter(FragmentManager fm, Activity activity, ViewPager pager) {
super(fm);
mContext = activity;
mActionBar = activity.getActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
正如我所说的,它仍然崩溃,任何想法在这一点?
开始原题:的 我有以下错误:构造FragmentPagerAdapter(FragmentManager)是不确定的*
Begin original question: I have the following error: "The constructor FragmentPagerAdapter(FragmentManager) is undefined"*
当我尝试执行以下命令行:
When I try to execute the following line:
超(activity.getFragmentManager());
这条线如下我的公共TabsAdapter(活动活动,ViewPager寻呼机){
这是延长 FragmentPagerAdapter 。
super(activity.getFragmentManager());
this line follows my public TabsAdapter(Activity activity, ViewPager pager) {
which is extending FragmentPagerAdapter.
基本上,我试图让我的8 +片段的应用程序(8片段,它承载了一组链接到每个片段动作条选项卡之一活性只有一个是在这里看到的简洁起见loginTab])。
Basically, I am trying to get my 8+ fragment app (8 fragment, one activity which hosts a set of ActionBar tabs linked to each fragment [only one is seen here for brevity's sake "loginTab"]).
下面是我的整个code:
Here’s my whole code:
编辑:网页,我已经使用,以供参考: http://developer.android.com/reference/android/support/v4/view/ViewPager.html
http://thepseudo$c$cr.word$p$pss.com/2011/10/05/android-page-swiping-using-viewpager/
package com.davekelley.polling;
import java.util.ArrayList;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
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.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
public class Polling extends Activity {
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayShowHomeEnabled(false);
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("Simple"),
LoginFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Simple"),
EconFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Simple"),
PoliticsFragment.class, null);
if (savedInstanceState != null) {
bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
}
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
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 mTabs = new ArrayList();
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.getFragmentManager());
mContext = activity;
mActionBar = activity.getActionBar();
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();
}
public int getCount() {
return mTabs.size();
}
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
public void onPageScrollStateChanged(int state) {
}
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);
}
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
}
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
}
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
}
}
}
任何想法是怎么回事,什么我可以做些什么来解决这个问题?我的片段类是目前十分简单,但最终会加载从SQL服务器的数据(在此期间,他们只返回一个简单的充气布局)。
Any idea what's going on here and what I can do to fix it? My Fragment classes are very simple at the moment, but will eventually load data from an SQL server (at the meantime, they just return a simple inflated layout).
我已经解决了这个问题。这是被要求的解决方案万千。
I have solved the problem. It was a myriad of solutions that was required.
首先,我不得不延长FragmentActivity。
First, I had to extend FragmentActivity.
public class Polling extends FragmentActivity {
然后,创建我TabsAdapter的时候,我不得不将呼叫发送到这个(在FragmentActivity本身):
Then, when creating my TabsAdapter, I had to send a call to this (the FragmentActivity itself):
mTabsAdapter = new TabsAdapter(this, mViewPager);
然后,使得在TabsAdapter子类的构造函数时,我不得不改变参数FragmentActivity而不是活动:
Then, when making the constructor in the TabsAdapter subclass, I had to change the parameter to FragmentActivity rather than Activity:
public TabsAdapter(FragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
最后,我不得不改变我的import语句在我的片段类来反映使用兼容性库,而不是常规的片段包:
Finally, I had to change my import statement in my Fragment classes to reflect the use of the compatibility library rather than the regular fragment package:
import android.support.v4.app.Fragment;
所以,现在我的ViewPager功能。当然,单击每个选项卡图标,标签之间不再起作用切换(所以我工作的这个解决方案,只要我发这条短信)。无论如何,现在的ViewPager功能,我可以在我的应用程序转移到更pressing问题。感谢LouisLouis启动我沿着正确的方向。我最终用CountingFragment.java帮我看到我的错误是,还有FragmentTabsPager.java从ActionBarSherlock的code,以确定是否需要指定FragmentActivity。
So, now my ViewPager functions. Of course, clicking on each individual tab icon to switch between tabs no longer works (so I am working on that solution as soon as I send this message). Regardless, now that the ViewPager functions, I can move on to more pressing issues in my app. Thanks to LouisLouis for starting me down the correct path. I eventually used CountingFragment.java to help me see where my errors were, as well as FragmentTabsPager.java from ActionBarSherlock's code to determine the need to specify FragmentActivity.