当我添加viewpager片段时,一切都很顺利,但当我移动到另一个应用程序并返回到我的应用程序时,我有一个奇怪的问题,getView或inflated是null。它显示了视图,但我无法访问它。
以下是将在ViewPager中使用的片段:
public class FragmentRestauranteBandejao extends Fragment {
JSONObject dados;
View inflated;
ExpandableListView cardapio;
List<String> refeicoes;
List<String> pratos_almoco;
List<String> pratos_janta;
Map<String, List<String>> bandejao = null;
ExpandableListAdapter adapter;
public static FragmentRestauranteBandejao newInstance(String restaurante){
FragmentRestauranteBandejao frag = new FragmentRestauranteBandejao();
Bundle args = new Bundle();
args.putString("restaurante", restaurante);
frag.setArguments(args);
return frag;
}
public void getDados(){
if(dados == null){
if(USPRestaurantes.getDados()){
dados = USPRestaurantes.getCardapioByRestaurante(getArguments().getString("restaurante"));
}else{
Log.d("debug", "Dados ainda não estão prontos");
return;
}
}
if(inflated == null){
Log.d("debug", "inflated eh null");
if(inflated == null){
return;
}
}
//... some code here that deals with the data that comes from a request made at USPRestaurantes.
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflated = inflater.inflate(R.layout.fragment_cardapio, container, false);
return inflated;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
inflated = view;
prepararCardapio();
getDados();
}
public void prepararCardapio(){
cardapio = (ExpandableListView) getView().findViewById(R.id.cardapio);
cardapio.setClickable(true);
refeicoes = new ArrayList<String>(Arrays.asList("Almoço","Jantar"));
pratos_almoco = new ArrayList<String>();
pratos_janta = new ArrayList<String>();
pratos_almoco.add("");
pratos_janta.add("");
bandejao = new LinkedHashMap<String, List<String>>();
bandejao.put(refeicoes.get(0), pratos_almoco);
bandejao.put(refeicoes.get(1), pratos_janta);
adapter = new ExpandableListViewAdapter(getActivity(), refeicoes, bandejao);
cardapio.setAdapter(adapter);
}
ViewPager片段:
public class FragmentBandejoes extends Fragment{
private String[] tabs = {"Central", "Química", "Física"};
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ArrayList<Fragment> paginasBandeco;
public int dia_semana = 0;
ProgressDialog dialog;
private TabPageIndicator mIndicator;
int number = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
paginasBandeco = new ArrayList<Fragment>();
paginasBandeco.add(FragmentRestauranteBandejao.newInstance("central"));
paginasBandeco.add(FragmentRestauranteBandejao.newInstance("quimica"));
paginasBandeco.add(FragmentRestauranteBandejao.newInstance("fisica"));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflated = inflater.inflate(R.layout.fragment_bandejoes, container, false);
return inflated;
}
public void notifyFragments(){
for(int i=0; i<3; i++){
((FragmentRestauranteBandejao) mAdapter.getItem(i)).getDados();
}
if(dialog != null){
dialog.dismiss();
}
}
public void getCardapios(boolean update){
if(!USPRestaurantes.getDados() || update){
boolean request_made = USPRestaurantes.getCardapio(getActivity());
}
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
getCardapios(false);
viewPager = (ViewPager) getView().findViewById(R.id.pager_bandejoes);
mAdapter = new TabsPagerAdapter(((MainActivity) getActivity()).fragmentManager, paginasBandeco, tabs);
viewPager.setAdapter(mAdapter);
mIndicator = (TabPageIndicator) getActivity().findViewById(R.id.indicator);
mIndicator.setViewPager(viewPager);
mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
mIndicator.setCurrentItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
最后是FragmentStatePagerAdapter:
public class FragmentBandejoes extends Fragment{
private String[] tabs = {"Central", "Química", "Física"};
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ArrayList<Fragment> paginasBandeco;
public int dia_semana = 0;
ProgressDialog dialog;
private TabPageIndicator mIndicator;
int number = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
paginasBandeco = new ArrayList<Fragment>();
paginasBandeco.add(FragmentRestauranteBandejao.newInstance("central"));
paginasBandeco.add(FragmentRestauranteBandejao.newInstance("quimica"));
paginasBandeco.add(FragmentRestauranteBandejao.newInstance("fisica"));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflated = inflater.inflate(R.layout.fragment_bandejoes, container, false);
return inflated;
}
public void notifyFragments(){
for(int i=0; i<3; i++){
((FragmentRestauranteBandejao) mAdapter.getItem(i)).getDados();
}
if(dialog != null){
dialog.dismiss();
}
}
public void getCardapios(boolean update){
if(!USPRestaurantes.getDados() || update){
boolean request_made = USPRestaurantes.getCardapio(getActivity());
}
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
getCardapios(false);
viewPager = (ViewPager) getView().findViewById(R.id.pager_bandejoes);
mAdapter = new TabsPagerAdapter(((MainActivity) getActivity()).fragmentManager, paginasBandeco, tabs);
viewPager.setAdapter(mAdapter);
mIndicator = (TabPageIndicator) getActivity().findViewById(R.id.indicator);
mIndicator.setViewPager(viewPager);
mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
mIndicator.setCurrentItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
其他信息:当我滑动到其他选项卡时,视图会重新创建,然后显示数据。 提前致谢
答案 0 :(得分:0)
据我所知,阅读你的代码就是你没有考虑fragment's lifecycle。
片段,作为活动,具有您应该使用的生命周期方法,以便它们正确呈现。我建议使用onResume回调,并检查是否有任何数据要显示。如果您确实在片段中显示它们。如果您在创建fragmet时没有执行此操作