android在fragment上实现寻呼机时出错

标签 android android-fragments android-viewpager

我在 fragment 内实现寻呼机,但出现此错误

The method getSupportFragmentManager() is undefined for the type
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;
    }
     }
     }

我在导入 fragment 时看到了一些问题,但正如您在我的代码中看到的那样,我是 android.support.v4.app...

还有什么问题吗?

谢谢!

最佳答案

不要调用 getSupportFragmentManager(),而是调用 getSherlockActivity().getSupportFragmentManager()。您现在尝试从 SherlockActivity 调用方法,但您位于 SherlockFragment 中;这就是为什么您需要首先获取 SherlockFragment 对象的引用...

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

关于android在fragment上实现寻呼机时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18786913/

相关文章:

javascript - 如何在点击 AREA HREF 标签时创建 Activity 事件(更改 BG 或创建边框)?

android - Activity 中的默认 fragment

android - 带页面指示器的无尽自动 ScrollView 寻呼机

java - 滑动时选项卡内容不会重叠

android - IllegalStateException:在使用 ViewPager onSaveInstanceState 后无法执行此操作

android - Apk外部数据,Android扩展

android - 选择 OpenGL ES 的适当错误

android - 如何在访问产品页面时提示用户下载应用程序?

android - 为什么我在 Android 中得到 "Cannot remove Fragment attached to a different FragmentManager"?

java - 确定要更新的 fragment