保存状态变为无效

时间:2017-09-07 10:32:02

标签: java android android-fragments android-savedstate

我正在尝试从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

1 个答案:

答案 0 :(得分:0)

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

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

另一个选择是只使用setter来获取所需的值。