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

Java 使用在另一个片段(NPE)中生成的值设置片段的TextView,java,android,nullpointerexception,textview,fragment,Java,Android,Nullpointerexception,Textview,Fragment,我有一个活动任务屏幕,它在创建时保存着碎片任务。 如果我按下按钮,这将触发屏幕活动的next()方法,从而将FragmentQuest替换为FragmentAction 到目前为止,更换工作仍在进行。问题是,在替换之后,我想在FragmentAction中将TextView设置为在 任务筛选活动 我的第一次尝试是将TextView设置在FragmentAction的onCreatView之外。这将导致在处理TextView时出现空指针异常 到目前为止,我已经看过:,和 下面看到的尝试导致代码中标

我有一个活动任务屏幕,它在创建时保存着碎片任务。 如果我按下按钮,这将触发屏幕活动的next()方法,从而将FragmentQuest替换为FragmentAction

到目前为止,更换工作仍在进行。问题是,在替换之后,我想在FragmentAction中将TextView设置为在 任务筛选活动

我的第一次尝试是将TextView设置在FragmentAction的onCreatView之外。这将导致在处理TextView时出现空指针异常

到目前为止,我已经看过:,和

下面看到的尝试导致代码中标记的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();
    }
}
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());
}
在FragmentAction类中

 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();
    }
}
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());
}
空对象引用上的Activity.findViewById(int)“”

这意味着您的
getActivity()
以某种方式返回
null
。 为了找出为什么会发生这种情况,google使用类似“
getActivity()
返回null”的方法是有关stackoverflow的此类问题的一个很好的例子

这是一个解决办法,以摆脱崩溃

  • 将活动字段添加到片段中

    活动

  • 重写
    onAttach()
    方法,并将值设置为
    活动
    字段

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

FrameLayout容器=(FrameLayout)activity.findViewById(R.id.flFragmentContainer)

空对象引用上的Activity.findViewById(int)“”

这意味着您的
getActivity()
以某种方式返回
null
。 为了找出为什么会发生这种情况,google使用类似“
getActivity()
返回null”的方法是有关stackoverflow的此类问题的一个很好的例子

这是一个解决办法,以摆脱崩溃

  • 将活动字段添加到片段中

    活动

  • 重写
    onAttach()
    方法,并将值设置为
    活动
    字段

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


FrameLayout容器=(FrameLayout)activity.findViewById(R.id.flFragmentContainer)

@Rotwang-OP基本上询问如何将数据传递给片段,并说之前的尝试导致了各种NPE(一个应该是证明自己的努力)。但是,由于解决方案不是“执行空检查”,因此,如果它是一个完全重复的问题,那么就不是最后一个链接所指的问题。请注意,这并不是关于想重新开放,我发布了答案,因为它太长了,无法发表评论,我投了更高的票,因为这是一个问得很好的问题,对于一个新手来说。如果我做对了,好的复制品可以通过指向大海捞针来帮助他人。@0X0nosugar
下面看到的尝试导致代码中标记的空点异常。
Enoug被标记为
是另一个NPE相关的复制问题
@Rotwang-好的,因此,如果这是我的问题,我应该在必要时添加空检查,然后问“为什么这个代码不起作用”@Rotwang-感谢您信任我避免/处理NPE;-)谢谢你的耐心。也许这就是问题的关键:对我来说,这是一个关于android机制的问题,被一些NPE所掩盖。通过空检查,应用程序不会崩溃。但我想“不会崩溃”不是“任务完成”。很抱歉重复,我不知道。对代码进行了一些修改,并创建了一个变通方法。而不是使用getActivity方法,该方法返回null。(我想因为nextAction()是在onCreate、onCreateView、onAttach等之前执行的)我现在将提交的数据存储在一个本地对象中,并使用这个对象在nextAction()方法之后填充TextView,在onCreateView中使用它。@Rotwang-OP基本上是询问如何将数据传递给片段,并说以前的尝试导致了各种NPE(一个应该是证明自己的努力)。但是,由于解决方案不是“执行空检查”,因此,如果它是一个完全重复的问题,那么就不是最后一个链接所指的问题。请注意,这并不是关于想重新开放,我发布了答案,因为它太长了,无法发表评论,我投了更高的票,因为这是一个问得很好的问题,对于一个新手来说。如果我做对了,好的复制品可以通过指向大海捞针来帮助他人。@0X0nosugar
下面看到的尝试导致代码中标记的空点异常。
Enoug被标记为
是另一个NPE相关的复制问题
@Rotwang-好的,因此,如果这是我的问题,我应该在必要时添加空检查,然后问“为什么这个代码不起作用”@Rotwang-感谢您信任我避免/处理NPE;-)谢谢你的耐心。也许这就是问题的关键:对我来说,这是一个关于android机制的问题,被一些NPE所掩盖。通过空检查,应用程序不会崩溃。但我想“不会崩溃”不是“任务完成”。很抱歉重复,我不知道。对代码进行了一些修改,并创建了一个变通方法。而不是使用getActivity方法,该方法返回null。(我想因为nextAction()是在onCreate、onCreateView、onAttach等之前执行的)我现在将提交的数据存储在一个本地对象中,并使用这个对象在nextAction()方法之后填充TextView,在onCreateView中使用它。谢谢你的建议。我曾尝试按照您的建议设置一个全局变量,但我无意中发现了这一点。但它不起作用。似乎nextAction()的代码是在onAttach()方法之前执行的。(至少在我的例子中,onAttach中的所有输出都不会显示在控制台中。)感谢您的建议。我已尝试按照您的建议设置一个全局变量