Android:通过片段交易获得白屏

Tir*_*lex 5 java android fragment android-fragments fragmenttransaction

我正在制作一个新闻应用程序,其中有一个片段将所有新闻存储在一个列表中,另一个片段具有视频列表。当您触摸新片段时,它将从名为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();
 }
Run Code Online (Sandbox Code Playgroud)

这是保存新闻并从MainActivity.java调用方法的Tab1.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;
    } 
}
Run Code Online (Sandbox Code Playgroud)

这是应该调用的片段,并显示空白屏幕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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Tir*_*lex 0

我通过删除对 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();
 }
Run Code Online (Sandbox Code Playgroud)

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;

        }
    }
Run Code Online (Sandbox Code Playgroud)

感谢 Eagle11 努力帮助我解决这个小问题,花了我一上午的时间才解决。我希望这个答案可以帮助像我这样的其他新手。