3

我作出了新闻应用,在那里我有保存列表中的所有的新闻片段和具有视频列表中的另一个片段。当你触摸一个新的,它会调用名为openNewsCont活性的方法,将与另一个替换当前的片段。安卓入门白屏片段交易

我遇到的proble是当我调用该方法,我得到了一个空白页面,我已经看过很多其他的代码,但我无法找到我的问题的解决方案。

下面是我从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(); 
} 

这里调用的方法是保持的新闻和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; 
    } 
} 

而这里Tab1.java是应该被调用并显示空白屏幕的片段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; 
    } 
} 

回答

0

One obvi对我而言,最重要的是你有ft.addToBackStack(null);两次。很确定它应该只在那里一次。

此外,我相信你需要ft.commit后直接调用fm.executePendingTransactions()()。

+0

是的我在编辑代码时犯了这个错误,我添加了代码,但是它仍然在加载一个空白页。我不知道,这就好像实际上并没有真正获得我现在真正迷失的片段 – Tirlex

0

我的,因为这是只由我TabAdapter,但只适用于像新闻,韦迪等标签所代表的片段需要消除对片段的V4支持解决了这个问题。来自未作为标签上进行管理,如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为他的努力帮助我,我花了整整一个上午来解决这个问题nooby。我希望这个答案能够帮助像我这样的新手。

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; 
}