我已经使用SherlockFragmentActivity
创建了一个标签式控件,并且在Fragment
之一内部,我正在尝试使用ViewPager。
SettingsActivity.java
public class SettingsActivity extends SherlockFragmentActivity
{
SherlockFragment faq = new FAQActivity();
SherlockFragment help = new HelpGuideActivity();
SherlockFragment options= new OptionsActivity();
SherlockFragment user = new UserActivity();
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.settings_activity);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle("");
actionBar.setLogo(R.drawable.home);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab faqTab = actionBar.newTab().setText("FAQ");
Tab helpTab = actionBar.newTab().setText("Instructions");
Tab optionsTab = actionBar.newTab().setText("Options");
Tab userTab = actionBar.newTab().setText("User");
faqTab.setTabListener(new TabListener(faq));
helpTab.setTabListener(new TabListener(help));
optionsTab.setTabListener(new TabListener(options));
userTab.setTabListener(new TabListener(user));
actionBar.addTab(userTab,true);
actionBar.addTab(faqTab);
actionBar.addTab(helpTab);
actionBar.addTab(optionsTab);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
finish();
return super.onMenuItemSelected(featureId, item);
}
public class TabListener implements ActionBar.TabListener {
Fragment fragment;
public TabListener(Fragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
}
HelpGuideActivity.java
public class HelpGuideActivity extends SherlockFragment {
ViewPager viewPager=null;
InstructionsAdapter adapter =null;
ImageView iv=null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
currentLayout = (RelativeLayout) inflater.inflate(R.layout.help_guide_activity, null, false);
return currentLayout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
GlobalContent.screenWidth=size.x;
GlobalContent.screenHeight=size.y;
viewPager = (ViewPager)currentLayout.findViewById(R.id.instructionPager);
adapter= new InstructionsAdapter(getChildFragmentManager());
viewPager.setAdapter(adapter);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
InstructionsAdapter.java
public class InstructionsAdapter extends FragmentPagerAdapter {
int[] resources = new int[]{R.drawable.ins_1,R.drawable.ins_2,R.drawable.ins_3,R.drawable.ins_4};
ArrayList片段;
public InstructionsAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
InstructionFragment fr =InstructionFragment.newInstance(resources[arg0]);
return fr;
}
public InstructionsAdapter(FragmentManager fm) {
super(fm);
fragments = new ArrayList<Fragment>();
InstructionFragment fr0 =InstructionFragment.newInstance(resources[0]);
InstructionFragment fr1 =InstructionFragment.newInstance(resources[1]);
InstructionFragment fr2 =InstructionFragment.newInstance(resources[2]);
InstructionFragment fr3 =InstructionFragment.newInstance(resources[3]);
fragments.add(fr0);
fragments.add(fr1);
fragments.add(fr2);
fragments.add(fr3);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
return fragments.get(arg0);
}
@Override
public int getCount() {
return 4;
}
}
InstructionFragment.java
public class InstructionFragment extends SherlockFragment {
int resID =-1;
public static final InstructionFragment newInstance(int resId)
{
InstructionFragment sf= new InstructionFragment();
sf.resID=resId;
return sf;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout lt = (RelativeLayout)inflater.inflate(R.layout.help_fragment, null);
ImageView iv = (ImageView)lt.findViewById(R.id.img);
iv.setImageBitmap(BitmapFactory.decodeResource(getResources(), resID));
return lt;
}
}
以上是不同的课程和我用过的实现。现在,如果我们切换到此帮助选项卡,那么它的工作正常。但是,如果我去其他标签然后我回来然后我跟随异常
12-02 21:32:49.869: E/AndroidRuntime(1371): FATAL EXCEPTION: main
12-02 21:32:49.869: E/AndroidRuntime(1371): java.lang.IllegalStateException: No activity
12-02 21:32:49.869: E/AndroidRuntime(1371): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1091)
12-02 21:32:49.869: E/AndroidRuntime(1371): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
12-02 21:32:49.869: E/AndroidRuntime(1371): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
12-02 21:32:49.869: E/AndroidRuntime(1371): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1492)
12-02 21:32:49.869: E/AndroidRuntime(1371): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
12-02 21:32:49.869: E/AndroidRuntime(1371): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-02 21:32:49.869: E/AndroidRuntime(1371): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-02 21:32:49.869: E/AndroidRuntime(1371): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
12-02 21:32:49.869: E/AndroidRuntime(1371): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
12-02 21:32:49.869: E/AndroidRuntime(1371): at android.os.Handler.handleCallback(Handler.java:730)
12-02 21:32:49.869: E/AndroidRuntime(1371): at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 21:32:49.869: E/AndroidRuntime(1371): at android.os.Looper.loop(Looper.java:137)
12-02 21:32:49.869: E/AndroidRuntime(1371): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-02 21:32:49.869: E/AndroidRuntime(1371): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 21:32:49.869: E/AndroidRuntime(1371): at java.lang.reflect.Method.invoke(Method.java:525)
12-02 21:32:49.869: E/AndroidRuntime(1371): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-02 21:32:49.869: E/AndroidRuntime(1371): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-02 21:32:49.869: E/AndroidRuntime(1371): at dalvik.system.NativeStart.main(Native Method)
那么,我想知道如何避免这种异常?如何在SherlockFragment中使用ViewPager?我在代码中犯了什么错误?
答案 0 :(得分:2)