[英]Android fragment getChildAt() crashing sometimes
我有一个正在膨胀并正确显示的片段。 我想更新一些视图,但是不是在onCreateView()
,而是在片段的方法showContent()
,因为此函数将被多次调用。
这行得通,但是由于某种原因有时会崩溃,尤其是在我第一次启动该应用程序时。 我得到的错误是:
`java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference`
这是片段的方法(为简单起见已更改):
public void showContent(ActionInfo currAction) {
//ActionInfo is a custom class
//'amount' is public int variable
//'who' is public String variable
switch ( currAction.amount ) {
case 1:
((TextView)((ViewGroup)((ViewGroup)getView().findViewById(R.id.Seat1)).getChildAt(0)).getChildAt(0)).setText(currAction.who);
break;
case 2:
((TextView)((ViewGroup)((ViewGroup)getView().findViewById(R.id.Seat2)).getChildAt(0)).getChildAt(0)).setText(currAction.who);
break;
//case...
}
//...
}
Seats的xml如下所示:
<RelativeLayout android:id="@+id/Seat1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<include layout="@layout/player_names"/>
</RelativeLayout>
layout / player_names是:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/player_names_card_view"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#795548"
android:textColor="#ffffff"
android:gravity="center"/>
</android.support.v7.widget.CardView>
似乎我尝试在onCreateView()
尚未发生时尝试更新它们,但我什至尝试设置标志来更改它,但它不起作用。
编辑:明确地说,我想从父活动中多次调用showContent()
,因此不从onViewCreated()
等调用
编辑2:经过所见并尝试后,我了解到这个问题几乎可以确定父活动中的哪一点都完全创建了父活动和片段,因此我可以安全地调用fragment.showContent()
来自上级活动
您可以访问RelativeLayout
Seat1
,后onCreateView()
与声明和链接relativelayout
上OnActivityCreated()
并在使用它showContent()
函数。
替换您的代码行,其中TextViewID是播放器xml布局中textview的ID。
((TextView)getView().findViewById(R.id.TextViewID)).setText(currAction.who);
“ include”只是复制布局以供重用,您可以将“ include”布局元素用作单个布局。
您可以尝试以下操作:
public class MyFragment extends Fragment{
boolean isViewCreated = false;
ActionInfo currAction;
.
.
.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
isViewCreated = true;
.
.
if(currAction != null){
showContent(currAction);
}
}
public void showContent(ActionInfo currAction) {
if(isViewCreated){
//ActionInfo is a custom class
//'amount' is public int variable
//'who' is public String variable
switch ( currAction.amount ) {
case 1:
((TextView)((ViewGroup)((ViewGroup)getView().findViewById(R.id.Seat1)).getChildAt(0)).getChildAt(0)).setText(currAction.who);
break;
case 2:
((TextView)((ViewGroup)((ViewGroup)getView().findViewById(R.id.Seat2)).getChildAt(0)).getChildAt(0)).setText(currAction.who);
break;
//case...
}
//...
} else {
this.currAction = currAction;
}
}
}
我添加了一个boolean
值以检查是否调用了onCreateView
,以便getView
不会返回null
值。
您是否探索了FragmentActivity::onResumeFragments()
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.