更改屏幕片段后崩溃

Crash after change screen fragment

我想用一个包发送数据并在另一个片段中检索它们。

我的问题是,当我在 Fragment Receive 中更改屏幕方向时,应用程序崩溃了。

这是我的一段代码:

片段接收:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

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

    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    ImageView networkImageView = (ImageView)view.findViewById(R.id.thumbnail);

    Bundle bundle = getArguments();

    textcontent = (TextView)view.findViewById(R.id.content);
    texttitle = (TextView)view.findViewById(R.id.title);
    textdate = (TextView)view.findViewById(R.id.date);
    textweb = (TextView)view.findViewById(R.id.web);



    textcontent.setText(Html.fromHtml(bundle.getString("article")));
    textdate.setText("le " + bundle.getString("date"));
    texttitle.setText(bundle.getString("title"));
    textweb.setText("Pour plus d'information : " + bundle.getString("urlweb")); }

旋转时,将重新创建您的片段,将再次调用 onCreateView() 并且 getArguments 将为空。所以你需要将数据保存在 onSaveInstanceState()onCreateView() 中,如果 savedInstanceState 为 null,则从 getArgument() 中获取数据,否则从 [=13] 中获取数据=].

将此行添加到清单中的 activity:

android:configChanges="keyboardHidden|orientation|screenSize"

使用 bundle

时应始终检查是否为 null
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_actuality, container, false);
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    ImageView networkImageView = (ImageView)view.findViewById(R.id.thumbnail);

        textcontent = (TextView)view.findViewById(R.id.content);
        texttitle = (TextView)view.findViewById(R.id.title);
        textdate = (TextView)view.findViewById(R.id.date);
        textweb = (TextView)view.findViewById(R.id.web);


        if(savedInstanceState!=null){  
        textcontent.setText(Html.fromHtml(bundle.getString("article")));
        textdate.setText("le " + bundle.getString("date"));
        texttitle.setText(savedInstanceState.getString("title"));
        textweb.setText("Pour plus d'information : "savedInstanceState.getString("urlweb"));
}