通过ViewModel在2个片段之间的setText()时为NPE

时间:2019-05-23 15:02:38

标签: android android-fragments nullpointerexception android-livedata android-viewmodel

我正在尝试在两个片段之间共享LiveData。我正在使用具有单个Activity的Nav体系结构组件。第一个片段是ListFragment,第二个片段是有关每个将list返回int值的listItem的Detail。但是当我切换回列表将引发java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'。

public class FirstFrag extends Fragment {

    public FirstFrag() {
        // Required empty public constructor
    }


    private SharedViewModel model;
    TextView finalScore;

    ListView myListView;
    String[] items;
    String[] bushido;
    String[] description;


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


    }

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

        model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
        model.getCurrentScore().observe(getViewLifecycleOwner(), new Observer<CharSequence>() {
            @Override
            public void onChanged(@Nullable CharSequence s) {

                finalScore.setText(s); <--(Basically this)
            }
        });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.activity_second, container, false);

        finalScore = view.findViewById(R.id.finalScoreView);

      //some code about my list and navigation...//

        return view;
    }

}

这是我的DetailFrag。

public class DetailFrag extends Fragment implements View.OnClickListener {

    ArrayList<Integer> selection = new ArrayList<>();
    TextView score_text;
    public int final_taskSelection = 0;
    private SharedViewModel model;


    public DetailActivity() {

    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

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

        model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
/*
        model.getCurrentScore().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {




            }
        });
*/
    }

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view =  inflater.inflate(R.layout.activity_detail, container, false);

        score_text = view.findViewById(R.id.scoreView);
        score_text.setEnabled(false);


      //some code..//

        return view;

    }

@Override
    public void onClick(View v) {

//some code..//

                score_text.setText("Score is confirmed");
                score_text.setEnabled(true);
                model.setCurrentScore(String.valueOf(final_taskSelection));


             break;

        }

    }

还有我的ViewModel类。

public class SharedViewModel extends ViewModel {


        private MutableLiveData<CharSequence> currentScore = new MutableLiveData<>();

        public LiveData<CharSequence> getCurrentScore(){
            return currentScore;
        }

        public void setCurrentScore(CharSequence finito) {
            currentScore.setValue(finito);

        }
}

0 个答案:

没有答案