编辑:回答了我自己的问题。
我的textView为空,我不明白为什么。我使用个人片段管理器可能是问题所在。我的ID是正确的,因为如果我在主活动上添加片段并从片段onCreate方法更改文本,则更改文本函数可以正常工作。什么不起作用是我有一个片段A添加到我的活动,我点击一个按钮,并计算我点击它的时间。然后我想将我的FragmentC的Textview更改为"柜台等于......"然后我的textview为空。我怀疑它为空的原因是它尚未添加到活动中。因此实际上没有调用onCreateView。我能以任何方式修复它吗?我应该在响应函数中调用Fragment.OnCreateView吗?
我的课程与textview:
public class FragmentC extends Fragment {
TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentc, container,false);
//textView = (TextView) view.findViewById(R.id.txtinfo2);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
textView = (TextView) getActivity().findViewById(R.id.txtinfo2);
}
public void changeText(String str){
if(textView==null)
Log.e("truc", "null txt");
textView.setText("jghjh");
}
}
Log给出txt null
fragmentc.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#854f98be">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment C"
android:id="@+id/txtinfo2"
android:layout_gravity="center_horizontal" />
</LinearLayout>
我把剩下的代码放在一边,因为我有自己的FragmentManager,可能是问题来自那里:
我的主要活动:
public class MainActivity extends ActionBarActivity implements Communicator{
private MonFragmentManager monFragmentManager;
public static final String TAG="truc";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
monFragmentManager = new MonFragmentManager
(getSupportFragmentManager(),R.id.container,new FragmentA(),new FragmentB(), new FragmentC());
monFragmentManager.add(1, false);
}
@Override
public void respond(String data) {
FragmentC frag = (FragmentC) monFragmentManager.getFragments()[2];
if(frag.getClass()==FragmentC.class)
Log.e("truc","fragment correct");
//log gives "fragment correct".
frag.changeText(data);
}
}
MyOwnFragmentManager类:
public class MonFragmentManager {
private FragmentManager fm;
private Fragment[] fragments;
int container;
public MonFragmentManager(FragmentManager fm){
this.fm = fm;
fragments = fm.getFragments().toArray(new Fragment[fm.getFragments().size()]);
}
public MonFragmentManager(FragmentManager fm, int container,Fragment...frags){
this.fm = fm;
this.container=container;
fragments = new Fragment[frags.length];
for(int i=0;i<fragments.length;i++){
fragments[i]=frags[i];
}
}
public void hideAllFragments(){...}
public void showFragment(int fragmentIndex, boolean addToBackStack) {...}
public void add(int fragmentIndex, boolean addToBackStack){
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.add(container,fragments[i]);
} else if(fragments[i].isAdded()) {
transaction.remove(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
public void removeAllFragments(){
FragmentTransaction transaction = fm.beginTransaction();
for(int i = 0; i < fragments.length; i++) {
transaction.remove(fragments[i]);
}
transaction.commit();
}
public Fragment[] getFragments() {
return fragments;
}
}
答案 0 :(得分:0)
在 onViewCreated 方法中执行findViewById()更安全。
示例代码:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
textView = (TextView) view.findViewById(R.id.txtinfo2);
注意:
答案 1 :(得分:0)
问题是我的片段没有添加到我的活动中(我没有在我的活动布局中定义片段)。一个简单的解决方法就是将它们全部添加到活动布局中,并使用hide / show而不是添加/删除。