gpt4 book ai didi

在 fragment 上实现寻呼机的android错误

转载 作者:行者123 更新时间:2023-11-29 16:05:38 24 4
gpt4 key购买 nike

我在 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 时看到了一些问题,但正如您在我的代码中看到的,im android.support.v4.app...

还有什么问题?

谢谢!

最佳答案

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

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com