2

フラグメント内にページャーを実装していますが、このエラーが発生します

メソッド getSupportFragmentManager() は、型
HowToUse_phoneに対して未定義です

の上

  mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

ここにコード、

package com.chocte.ks_documents;
import java.util.Locale;
import android.os.Bundle;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import android.support.v4.app.DialogFragment; 
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; 

public class HowToUse_phone extends SherlockFragment{

private LinearLayout lLayoutFrgHow;

ViewPager mViewPager;
SectionsPagerAdapter mSectionsPagerAdapter;


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    //return inflater.inflate(R.layout.activity_how_phone, container, false);

    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    //mViewPager = (ViewPager) findViewById(R.id.pager);

    mViewPager = (ViewPager) getView().findViewById(R.id.pager);

    mViewPager.setAdapter(mSectionsPagerAdapter);

    //get the layou8t
    lLayoutFrgHow=(LinearLayout) inflater.inflate(R.layout.activity_how_phone, container, false);
    return lLayoutFrgHow;

}   

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            //return getString(R.string.title_section1).toUpperCase(l);
            return "a";

        case 1:
            //return getString(R.string.title_section2).toUpperCase(l);
            return "b";
        case 2:
            //return getString(R.string.title_section3).toUpperCase(l);
            return "c";
        }
        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;
    }
     }
     }

フラグメントをインポートするときにいくつかの問題がありましたが、私のコードでわかるように、android.support.v4.app...

他に何が問題ですか?

ありがとう!

4

2 に答える 2

2

レイアウトを膨らませる前に、ビューを取得しようとしています。

R.layout.activity_how_phone に、ここで取得しようとしている ViewPager への Id が含まれていると仮定します。

mViewPager = (ViewPager) getView().findViewById(R.id.pager);

次のようになります。

   mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


   lLayoutFrgHow=(LinearLayout) inflater.inflate(R.layout.activity_how_phone, container, false);
   mViewPager = (ViewPager) lLayoutFrgHow.findViewById(R.id.pager);

   mViewPager.setAdapter(mSectionsPagerAdapter);

   //get the layout

   return lLayoutFrgHow;

それに加えて、なぜ Fragment 内で ViewPager を使用しているのですか? 通常、アクティビティ内で使用され、フラグメントをホストしています。

さらに、2 種類のフラグメントを使用します。ViewPager ではサポート v4 のフラグメントを使用しており、クラスは SherlockFragment です。

于 2013-09-13T13:07:35.907 に答える
2

getSupportFragmentManager()callを呼び出す代わりにgetSherlockActivity().getSupportFragmentManager()。からメソッドを呼び出そうとしてSherlockActivityいますが、あなたはSherlockFragment;にいます。そのため、最初にオブジェクトへの参照を取得する必要がありSherlockFragmentます ...

しかし、上記はコンパイルエラーのみを修正します...コードに他の問題があるかどうかはわかりません。前の回答によると、フラグメント ビューを適切なタイミングでインスタンス化していませんでした。

于 2013-09-13T13:11:14.467 に答える