2

我在片段中实现寻呼机,但出现此错误

对于
HowToUse_phone类型,方法 getSupportFragmentManager() 未定义

  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;

除此之外,你为什么在片段中使用 ViewPager?通常它在一个 Activity 中使用,并且它托管 Fragments。

此外,您还使用了 2 种不同的片段。在您的 ViewPager 中,您使用的是 support v4 中的 Fragments,而您的 Class 是一个 SherlockFragment。

于 2013-09-13T13:07:35.907 回答
2

而不是调用getSupportFragmentManager()call getSherlockActivity().getSupportFragmentManager()。你现在正试图调用一个方法,SherlockActivity但你在SherlockFragment;这就是为什么你需要先得到一个 ref 来SherlockFragment反对......

但上面只会修复编译错误......我不确定你的代码中是否还有其他问题。根据先前的答案,您没有在适当的时间实例化您的片段视图。

于 2013-09-13T13:11:14.467 回答