保存的状态被取消

Saved State getting nullified

我正在尝试从 savedInstanceState 中获取保存在 newInstace() 中并且需要在 onCreateView() 中获取的详细信息。

问题:- 当我尝试获取详细信息时,它会抛出 excpetion.

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

我无法确定为什么 savedInstanceState 得到 nullifiedGCed

缺陷代码:-

public class FeedDetailsFragment extends Fragment {


    public static final String TAG = FeedDetailsFragment.class.getSimpleName();
    private static String KEY_NAME="1";
    private static String KEY_TEXT="2";
    private static String KEY_IMAGEURL="3";
    private static String KEY_TITLE="4";
    private static String KEY_TIME="5";
    private static String KEY_DESC="6";

    public static FeedDetailsFragment newInstance(DataModel data){
        FeedDetailsFragment detailsFragment=new FeedDetailsFragment();
        Log.d(TAG, "Is Data Null? " + ((data == null) ? "Yes" : "No") + " For example: name is " + data.getName());
        Bundle bundle=new Bundle();
        bundle.putString(KEY_NAME,data.getName());
        bundle.putString(KEY_TEXT,data.getText());
        bundle.putString(KEY_IMAGEURL,data.getImageUrl());
        bundle.putString(KEY_TITLE,data.getTitle());
        bundle.putLong(KEY_TIME,data.getTime());
        bundle.putString(KEY_DESC,data.getDescription());
        detailsFragment.setArguments(bundle);
        fragtag.put(TAG,detailsFragment);
        return detailsFragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle state) {
        View view = inflater.inflate(R.layout.fragment_feeddetail, container, false);
        TextView detail_title=view.findViewById(R.id.detail_title);
        TextView detail_text=view.findViewById(R.id.detail_text);
        ImageView detail_image=view.findViewById(R.id.detail_image);
        TextView detail_name=view.findViewById(R.id.detail_name);
        TextView detail_desc=view.findViewById(R.id.detail_desc);
        Log.d(TAG, "Is State Null? " + ((state == null) ? "Yes" : "No"));
            detail_title.setText(state.getString(KEY_TITLE));
            detail_text.setText(state.getString(KEY_TEXT));
            requestImage(detail_image,state.getString(KEY_IMAGEURL));
            detail_name.setText(state.getString(KEY_NAME));
            detail_desc.setText(state.getString(KEY_DESC));
        return view;
    }
}

LogCat输出:这个很关键。你可能想看看这个。

09-07 15:53:38.374 14729-14729/io.github.abhirojp.myfeed_android D/MainActivity: Display Details
09-07 15:53:38.379 14729-14729/io.github.abhirojp.myfeed_android D/FeedDetailsFragment: Is Data Null? No For example: name is John Zeratsky
09-07 15:53:38.399 14729-14729/io.github.abhirojp.myfeed_android D/FeedDetailsFragment: Is State Null? Yes
09-07 15:53:38.399 14729-14729/io.github.abhirojp.myfeed_android D/AndroidRuntime: Shutting down VM

Bundle state 不用于将初始数据传递给片段,而是用于在片段被销毁并重新创建后恢复片段的状态。

由于您使用 detailsFragment.setArguments(bundle); 将 Bundle 设置为 Fragment,因此您应该使用 this.getArguments() 将此数据放入片段中。

另一种选择是简单地使用设置器来获取您需要的值。