【问题标题】:Android: Getting white screen with fragment transactionAndroid:通过片段事务获取白屏
【发布时间】:2014-04-30 14:50:45
【问题描述】:

我正在制作一个新闻应用程序,其中有一个片段将所有新闻保存在一个列表中,另一个片段包含视频列表。当您触摸一个新的片段时,它会从名为 openNewsCont 的活动中调用一个方法,该方法将用另一个片段替换当前片段。

我遇到的问题是,当我调用该方法时出现空白屏幕,我查看了许多其他代码,但找不到解决问题的方法。

这是我从 MainActivity.java 调用的方法

public void openNewsCont(String text) {

    android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.pager, new NewsContent() );
    ft.addToBackStack(null);
    ft.commit();
    fm.executePendingTransactions();
 }

这里是Tab1.java,它保存新闻并从MainActivity.java调用方法

package com.example.link_test;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;

    public class Tab1 extends Fragment {

          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {

              final View android = inflater.inflate(R.layout.tab1, container, false);             
              LinearLayout newLink = (LinearLayout) android.findViewById(R.id.news1);
              newLink.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    String text = "Text from fragment";
                    MainActivity ma = (MainActivity) getActivity();
                    ma.openNewsCont(text);
                }
                });

              return android;
    } 
}

这是应该被调用并显示一个空白屏幕 NewsContent.java 的片段

package com.example.link_test;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class NewsContent extends Fragment {
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.news_content, container, false);
            return view;
    }
}

【问题讨论】:

    标签: java android android-fragments fragment fragmenttransaction


    【解决方案1】:

    将片段替换为:

        Fragment feeddetailFragment = new FeedDetailFragment();
    feedFragment.getFragmentManager().beginTransaction()
    .replace(R.id.frame_container, feeddetailFragment,Constant.FRAGMENT_FEEDDETAIL)
    .addToBackStack(null)
    .commit();
    

    它会起作用的..

    如果仍然无法正常工作,请尝试检查您要在替换后显示的片段,无论该片段的视图是否为空

    if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }
        try {
    
            view = inflater.inflate(R.layout.fragment_business_details,
                    container, false);
    
            initializeViewObjects();
            setListeners();
    
        } catch (InflateException e) {
    
            return view;
        }
    
        return view;
    }
    

    【讨论】:

      【解决方案2】:

      我通过删除对片段 v4 的支持解决了这个问题,因为这仅是我的 TabAdapter 需要的,但仅适用于新闻、视频等选项卡中表示的片段。来自不作为标签管理的片段,例如 NewsContent.java

      这是我从 Tab1 打开另一个片段的方法应该是这样的。

      public void openNewsCont() {
      
          NewsContent nc = new NewsContent();
          FragmentManager fm = getFragmentManager();
          FragmentTransaction ft = fm.beginTransaction();
          ft.replace(R.id.container, nc);
          ft.addToBackStack(null);
          ft.commit();
       }
      

      NewsContent 应该是这样的:

      package com.example.link_test;
      
          import android.app.Fragment;
          import android.os.Bundle;
          import android.view.LayoutInflater;
          import android.view.View;
          import android.view.ViewGroup;
      
          public class NewsContent extends Fragment {
      
              public View onCreateView(LayoutInflater inflater, ViewGroup container,
                      Bundle savedInstanceState) {
              // Inflate the layout for this fragment
              final View android = inflater.inflate(R.layout.news_content, container, false);
      
      
              return android;
      
              }
          }
      

      感谢 Eagle11 帮助我解决这个让我花了整个上午才解决的棘手问题。我希望这个答案可以帮助像我这样的其他新手。

      【讨论】:

        【解决方案3】:

        您可以在托管片段的活动上使用此代码。它检查您的堆栈并重新启动片段。这是我决定最终处理这个问题的方法之一。

        @Override
            public void onBackPressed() {
                int fragments = getFragmentManager().getBackStackEntryCount();
                Toast.makeText(this, String.valueOf(fragments), Toast.LENGTH_LONG).show();
        
                if (fragments == 0) {
                    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.container, new NewsContent());
                    fragmentTransaction.commit();
        
                    super.onBackPressed();
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-10
          • 1970-01-01
          • 2022-01-10
          • 2016-02-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多