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;
    }
}

そして、これは最初の 1 つのフラグメントです。

     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

あなたのコードは正しいようです..あなたの問題はビルドパスとlibs android-support-v4.jarに関連していると思います.libsフォルダーにjarファイルがある場合、プロジェクトはコンパイルされますが、実行時にプロジェクトがクラッシュするビルドパスに正しく含めないでください。

プロパティで確認できますか - Java ビルド パス - 注文とエクスポート - android-support-v4.jar が存在し、チェックされている必要があります

于 2013-10-30T15:16:44.853 に答える