0

好吧,我是 Android 新手,我正在尝试制作一个 3 页的滑动应用程序。我从 eclipse 中创建了一个新项目,并选择了带有固定选项卡和可滚动页面的项目。我删除了这些选项卡,因为我想要一个只有可滚动页面的应用程序。这是代码

MainActivity 类

@SuppressLint("ValidFragment")
            public class MainActivity extends FragmentActivity  {


SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

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

    final int PAGE_COUNT=3;

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(3);
    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {

                }
            });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.

    }
}




/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null; 
        switch(position) {
        case 0:
            fragment = Fragment.instantiate(getBaseContext(), first.class.getName());
            break;
        case 1:
            fragment = Fragment.instantiate(getBaseContext(), second.class.getName());
            break;              
        case 2:
            fragment = Fragment.instantiate(getBaseContext(), MainActivity.class.getName());
            break;          }
    return fragment;    
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }
    public class good  extends Fragment{

    }


    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        case 2:
            return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy,
                container, false);
        TextView dummyTextView = (TextView) rootView
                .findViewById(R.id.section_label);
        dummyTextView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
        return rootView;
    }
}

这是一个片段 first.java

     public class first extends FragmentActivity{


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

        // here you can load whatever layout you want for your fragment
        View v = inflater.inflate(R.layout.prva, container, false);
        return v;
  }}

我得到的错误:

10-30 14:46:20.355: E/AndroidRuntime(7828): android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.swipe.MainActivity$SectionsPagerAdapter$good: make sure class name exists, is public, and has an empty constructor that is public
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.support.v4.app.Fragment.instantiate(Fragment.java:413)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at com.example.swipe.MainActivity$SectionsPagerAdapter.getItem(MainActivity.java:91)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.view.View.measure(View.java:15473)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5056)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.view.View.measure(View.java:15473)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.view.View.measure(View.java:15473)
10-30 14:46:20.355: E/AndroidRuntime(7828):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5056)
4

1 回答 1

0

您的代码似乎是正确的.. 我认为您的问题与构建路径和库有关 android-support-v4.jar 有时(在 eclipse 中对我来说)如果您在 libs 文件夹中有一个 jar 项目编译,但如果您不包括在运行时项目崩溃的构建路径中更正它。

你能检查一下属性 - java 构建路径 - 订购和导出 - android-support-v4.jar 必须存在检查和向上

于 2013-10-30T15:16:44.853 回答