如何纠正片段未启动的问题?
我正在启动一个活动,该活动托管一个带有项目列表的片段。
当单击某个项目时。布局是否针对 ThreePane 进行了测试。
public void onFeedSelected(Feed feed) {
if (isThreePane) {
addItemsFragment(feed);
}
else {
Intent i=new Intent(this, ItemsActivity.class);
i.putExtra(ItemsActivity.EXTRA_FEED_KEY, feed.getKey());
startActivity(i);
}
}
正如您所看到的,如果它不是三个窗格,那么它会启动一个活动。这在普通手机上运行良好。
但如果它是平板电脑屏幕,则活动不会在片段中启动。
这是我的 AddFeed() 方法。
public void addItemsFragment(Feed feed) {
FragmentManager fragMgr=getSupportFragmentManager();
ItemsFragment items=(ItemsFragment)fragMgr.findFragmentById(R.id.second_pane);
FragmentTransaction xaction=fragMgr.beginTransaction();
if (items==null) {
items=new ItemsFragment(true);
items.setOnItemListener(this);
xaction
.add(R.id.second_pane, items)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit();
}
else {
ContentFragment content=
(ContentFragment)fragMgr.findFragmentById(R.id.third_pane);
if (content!=null) {
xaction.remove(content).commit();
fragMgr.popBackStack();
}
}
我不明白为什么它没有启动。当我单击该项目时,它在平板电脑设备上没有任何反应。
编辑:
private boolean isThreePane=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FeedsFragment feeds
=(FeedsFragment)getSupportFragmentManager()
.findFragmentById(R.id.feeds);
feeds.setOnFeedListener(this);
isThreePane=(null!=findViewById(R.id.second_pane));
if (isThreePane) {
feeds.enablePersistentSelection();
}
}
i am launching a activity hosting a fragment with a list of items..
When an item is clicked. The layout is tested for threePane or not.
public void onFeedSelected(Feed feed) {
if (isThreePane) {
addItemsFragment(feed);
}
else {
Intent i=new Intent(this, ItemsActivity.class);
i.putExtra(ItemsActivity.EXTRA_FEED_KEY, feed.getKey());
startActivity(i);
}
}
As you can see if it is not three pane then it launches an activity..This works fine on regular phones.
But if it is a tablet screen the activity isnt launched in the fragment.
Here is my AddFeed() method.
public void addItemsFragment(Feed feed) {
FragmentManager fragMgr=getSupportFragmentManager();
ItemsFragment items=(ItemsFragment)fragMgr.findFragmentById(R.id.second_pane);
FragmentTransaction xaction=fragMgr.beginTransaction();
if (items==null) {
items=new ItemsFragment(true);
items.setOnItemListener(this);
xaction
.add(R.id.second_pane, items)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit();
}
else {
ContentFragment content=
(ContentFragment)fragMgr.findFragmentById(R.id.third_pane);
if (content!=null) {
xaction.remove(content).commit();
fragMgr.popBackStack();
}
}
I dont understand why its not being launched..When i click the item it does nothing on a tablet device.
EDIT:
private boolean isThreePane=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FeedsFragment feeds
=(FeedsFragment)getSupportFragmentManager()
.findFragmentById(R.id.feeds);
feeds.setOnFeedListener(this);
isThreePane=(null!=findViewById(R.id.second_pane));
if (isThreePane) {
feeds.enablePersistentSelection();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我根本不熟悉 Fragment API,所以这完全是在黑暗中拍摄...
在平板电脑上设置为 true,所以这意味着
findViewById(R.id.second_pane)
不是返回空值。您的 xml 中是否有硬编码的 secondary_pane 视图?应用程序如何决定是否应显示 secondary_pane 视图?它完全基于屏幕尺寸吗?您应该使用这样的东西:
而不是 findViewById 吗?再说一遍,我对fragments api一无所知,如果这是个愚蠢的问题,我很抱歉。
I am not familiar with the Fragment API at all so this is total shot in the dark...
is getting set to true on the tablet, so that means
findViewById(R.id.second_pane)
is not returning null. Do you have this second_pane view hard coded in your xml? How does the app decide whether or not the second_pane view should be present? Is it based on screen size at all?Should you be using something like this:
Instead of findViewById? Again I know nothing about fragments api, sorry if that is silly question.