Android片段getChildAt()有时会崩溃

时间:2016-08-11 08:35:02

标签: java android android-fragments findviewbyid

我有一个正在膨胀并正确显示的片段。我想更新一些视图,但不是在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()

4 个答案:

答案 0 :(得分:0)

您可以在RelativeLayout之后访问Seat1 onCreateView(),并在relativelayout上声明并关联OnActivityCreated()并在showContent()函数中使用它

答案 1 :(得分:0)

用你的代码行替换你的代码行,其中TextViewID是播放器xml布局中textview的id。

 ((TextView)getView().findViewById(R.id.TextViewID)).setText(currAction.who); 

&#34;包括&#34;只需复制布局以便重复使用,您可以使用&#34; include&#34;布局元素作为单一布局。

答案 2 :(得分:0)

您可以尝试以下内容:

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值。

答案 3 :(得分:0)

你有没有探索FragmentActivity::onResumeFragments()