更改屏幕片段后发生崩溃

更改屏幕片段后发生崩溃

问题描述:

我想用捆绑包发送数据并在另一个片段中检索它们。更改屏幕片段后发生崩溃

我的问题是,当我改变屏幕的方向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")); } 

此行添加到您的活动清单:

android:configChanges="keyboardHidden|orientation|screenSize" 
+0

工作谢谢你的时候 – user3083055 2015-01-21 09:32:47

在转动,你的片段重新创建,onCreateView()将会再次调用和getArguments将为空。因此,您需要将数据保存在onSaveInstanceState()onCreateView()中,如果savedInstanceState为空,则会添加数据,否则将从getArgument()获取数据,否则您将从savedInstanceState获取数据。

您应经常检查空以集束

@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")); 
}