我在片段中实现了一个寻呼机,但是我得到了这个错误
方法getSupportFragmentManager()未定义类型
HowToUse_phone
on
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 ...
还有什么问题?
谢谢!
答案 0 :(得分: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?通常它使用的是一个活动,它正在托管碎片。
此外,您还使用了2种不同的碎片。在您的ViewPager中,您使用的是支持v4中的碎片,而您的类是SherlockFragment。
答案 1 :(得分:2)
而不是致电getSupportFragmentManager()
致电getSherlockActivity().getSupportFragmentManager()
。您现在正尝试从SherlockActivity
调用方法,但您在SherlockFragment
;这就是为什么你需要先获得一个参考SherlockFragment
对象......
但上面只会修复编译错误......我不确定你的代码中是否还有其他任何问题。根据之前的回答,您没有在适当的时间实例化您的片段视图。