使用另一个片段(NPE)中生成的值设置片段的TextView

时间:2017-06-30 15:57:58

标签: java android nullpointerexception textview fragment

我有一个Activity QuestScreen,它在创建时包含Fragment FragmentQuest 。 如果我按下按钮,则触发屏幕活动的 next()方法,导致用 FragmentAction 替换 FragmentQuest

到目前为止替换工作。问题是,在替换之后我想在FragmentAction中设置一个TextView,以获得在 QuestScreen活动。

我的第一次尝试是在 FragmentAction onCreatView 之外设置 TextView 。这会导致Nullpointer异常处理TextView。

到目前为止,我已查看过:ThisThatThisThat

下面的尝试导致代码中标记了NullpointerException。

在QuestScreen类中

 private void next(){

    //The needed value for the TextView is stored in nextTransition
    Transition nextTransition;
    //Check if an entry is in the list
    if(quest.getTransitionsAbleToFire().size() > 0){
        nextTransition = quest.getTransitionsAbleToFire().get(0);

        fragmentAction = (FragmentAction) fm.findFragmentByTag(TAG_FRAGMENT_ACTION);

        // create the fragment and data the first time
        if (fragmentAction == null) {
            Log.d("QuestScreen onCreate" , "create fAction for first time");
            // add the fragment
            fragmentAction = (FragmentAction) Fragment.instantiate(this, FragmentAction.class.getName(), null);

            fm.beginTransaction().add(fragmentAction, TAG_FRAGMENT_ACTION).commit();
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.flFragmentContainer, fragmentAction);
            fragmentTransaction.commit();
        }

        //Triggers the corresponding activity in fragmentAction, to set the TextView to a value which is stored in nextTransition
        fragmentAction.nextAction(nextTransition);
        nextTransition.fire();

    }
    else{
        CharSequence text = "Quest END";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(getApplicationContext(), text, duration);
        toast.show();
    }
}

在FragmentAction类中

public class FragmentAction extends Fragment {

private TextView txtYourQuest;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // CREATE VIEWS HERE

    View layout = inflater.inflate(R.layout.content_action_fragment, container, false);
    txtYourQuest = (TextView) layout.findViewById(R.id.txtV_Your_Quest);
    return layout;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

public void nextAction(Transition prmTransition){

    // --> Here in the enxt Line comes the NPE :  java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference

    FrameLayout container = (FrameLayout) getActivity().findViewById(R.id.flFragmentContainer);
    LayoutInflater.from(getActivity())
            .inflate(R.layout.content_action_fragment, container, false);

    txtYourQuest.setText("Your " + prmTransition.getAction().getPosition() + ". is to:\n "
    + prmTransition.getAction().getActionName());
}

1 个答案:

答案 0 :(得分:0)

  

对象引用

上的Activity.findViewById(int)'

这意味着您的getActivity()会以某种方式返回null。 要找出答案,为什么会发生这种情况,谷歌就像“getActivity()返回null”。 Here 就是这种关于stackoverflow的问题的一个很好的例子。

这是摆脱崩溃的解决方法。

  • 为您的片段添加活动字段。

    活动活动;

  • 覆盖onAttach()方法并将值设置为activity字段。

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }
    
  • getActivity()方法替换为activity变量。

FrameLayout container =(FrameLayout)activity.findViewById(R.id.flFragmentContainer);