Android:使用片段交易获取白色屏幕 [英] Android: Getting white screen with fragment transaction
问题描述
我正在制作一个新闻应用程序,其中包含一个片段,用于保存列表中的所有新闻以及另一个包含视频列表的片段。当你触摸一个新的时候,它会调用一个名为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;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.View;
导入android.view.View.OnClickListener;
导入android.view.ViewGroup;
导入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;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.View;
导入android.view.ViewGroup;
公共类NewsContent延伸片段{
@覆盖
公共视图onCreateView(LayoutInflater充气,ViewGroup中的容器中,
捆绑savedInstanceState){
视图视图=充气.inflate(R.layout.news_content,container,false);
返回视图;
}
}
replace片段为:
片段feeddetailFragment = new FeedDetailFragment();
feedFragment.getFragmentManager()的BeginTransaction()
.replace(R.id.frame_container,feeddetailFragment,Constant.FRAGMENT_FEEDDETAIL)
.addToBackStack(空)
.commit();
这会起作用..
如果仍然无法正常工作,然后尝试检查您要在替换后显示的片段,以确定该片段的视图是否为空
if (view!= null){
ViewGroup parent =(ViewGroup)view.getParent();
if(parent!= null)
parent.removeView(view);
}
尝试{
view = inflater.inflate(R.layout.fragment_business_details,
container,false);
initializeViewObjects();
setListeners();
} catch(InflateException e){
return view;
}
返回视图;
}
I am making a news app where I have a fragment that holds all the news in a list and another fragment that have videos list. When you touch a new it will call a method from the activity named openNewsCont that will replace the current fragment with another one.
The proble I am having is when I call that method I get a blank screen, I have looked many other codes but I couldn't find the solution for my problem.
Here is the method I call from the 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();
}
Here is the Tab1.java that hold the news and calls the method from the 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;
}
}
And here is the fragment that should be called and shows a blank screen 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;
}
}
replace fragment as:
Fragment feeddetailFragment = new FeedDetailFragment();
feedFragment.getFragmentManager().beginTransaction()
.replace(R.id.frame_container, feeddetailFragment,Constant.FRAGMENT_FEEDDETAIL)
.addToBackStack(null)
.commit();
It will work..
If Still not working then try to check in the fragment which you want to show after replacement whether that fragment's view is null or not
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;
}
这篇关于Android:使用片段交易获取白色屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!