问题描述
我似乎无法改变一个TextView是从我的fragmentActivity一个片段的文字。此外,我曾尝试 frag.getView()。findViewById
其中,而我会觉得是做了正确的方式,返回null,抛出一个空指针异常。
如果我写在片段本身设置文本并使用getView(),我也得到空指针异常。方法
另外值得注意的,加载的第一个片段,称为由片段:姓名,负载并填充与此相同的code文本。它只有在 FragmentTransaction.replace不起作用()
被称为
下code不抛出任何异常,但是文本保持空白。
公共无效onTabSelected(标签选项卡,FragmentTransaction英尺){
如果(tab.getTag()。等于(信息)){
DisplayEventInfoFragment FRAG =新DisplayEventInfoFragment();
ft.replace(R.id.detailsfragment,FRAG);
最终的TextView mTimeView =(的TextView)findViewById(R.id.ievent_title);
最终的TextView mVenueView =(的TextView)findViewById(R.id.ievent_venue);
最终的TextView mDescView =(的TextView)findViewById(R.id.ievent_description);
mTimeView.setText(thisEvent.get(theTime)+ - + thisEvent.get(ENDTIME));
mVenueView.setText(thisEvent.get(场地));
mDescView.setText(thisEvent.get(说明)); }否则如果(tab.getTag()。等于(位置)){
// DisplayEventMapFragment FRAG =新DisplayEventMapFragment();
//ft.replace(R.id.detailsfragment,FRAG);
}}
在查询主视图或片段的任何意见子,则必须等到Fragment.onCreateView()被调用。
这似乎很奇怪父活动负责修改片段的布局组件。对使用片段的主要优点是它们可以自我控制,整个应用程序重复使用。
或许更可行的办法是重写你的碎片onActivityCreated(),或在onStart()(这是onCreateView()之后调用),并让他们填写自己的TextViews那里。
I cannot seem to change the text in a textview that is in an fragment from my fragmentActivity. Additionally I have tried frag.getView().findViewById
which while I would think is the right way to do it, returns null and throws a nullpointer exception.
If I write methods in the Fragment itself to set the text and use getView(), I also get nullpointer exceptions.
Also of note, the first fragment loaded, called by the fragment:name, loads and populates text with this same code. It only does not work after FragmentTransaction.replace()
has been called
The code below does not throw any exceptions, however the text remains blank.
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(tab.getTag().equals("info")) {
DisplayEventInfoFragment frag = new DisplayEventInfoFragment();
ft.replace(R.id.detailsfragment, frag);
final TextView mTimeView = (TextView)findViewById(R.id.ievent_title);
final TextView mVenueView = (TextView)findViewById(R.id.ievent_venue);
final TextView mDescView = (TextView)findViewById(R.id.ievent_description);
mTimeView.setText(thisEvent.get("theTime")+" - "+thisEvent.get("endTime"));
mVenueView.setText(thisEvent.get("venue"));
mDescView.setText(thisEvent.get("description"));
} else if(tab.getTag().equals("location")) {
//DisplayEventMapFragment frag = new DisplayEventMapFragment();
//ft.replace(R.id.detailsfragment, frag);
}
}
When querying the main View or any sub Views of a Fragment, you must wait until Fragment.onCreateView() has been called.
It seems odd that the parent Activity is responsible for modifying layout components of Fragments. One of the main advantages of using Fragments is that they can be self contained and reused throughout your application.
Perhaps a more viable approach would be to override onActivityCreated() or onStart() of your Fragments (which are called after onCreateView()), and have them populate their TextViews there.
这篇关于不能更改TextView的片段通过setText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!