Activity是这样
public class sssj_krzt_show extends FragmentActivity
implements HeadlinesFragment.OnHeadlineSelectedListener{
private Button btn_BACK;
TextView textView_Title;
private ArrayList<String> infoList = new ArrayList<String>();
Bundle bundle = new Bundle();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
...
}
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Capture the article fragment from the activity layout
KRZT_Cont articleFrag = (KRZT_Cont)
getSupportFragmentManager().findFragmentById(R.id.krzt_cont_frag);
//传递String
bundle.putString("String", "ok");
articleFrag.setArguments(bundle);[/color]
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
}
}
...
}
Fragment是这样
public class KRZT_Cont extends Fragment{
final static String DLL_ARG_POSITION = "krzt_position";
int mCurrentPosition = 0;
LinearLayout linechart_a;
GridView gridview;
Context context;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// If activity recreated (such as from screen rotate), restore
// the previous article selection set by onSaveInstanceState().
// This is primarily necessary when in the two-pane layout.
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(DLL_ARG_POSITION);
}
context = getActivity().getApplicationContext() ;
// Inflate the layout for this fragment
return inflater.inflate(R.layout.krzt_view, container, false);
}
@Override
public void onStart() {
super.onStart();
// During startup, check if there are arguments passed to the fragment.
// onStart is a good place to do this because the layout has already been
// applied to the fragment at this point so we can safely call the method
// below that sets the article text.
linechart_a = (LinearLayout) getActivity().findViewById(R.id.krzt_linea_a);
gridview = (GridView) getActivity().findViewById(R.id.gridview);
//接受info
Bundle bundle = this.getArguments();
String s = bundle.getString("String");[/color]
if (mCurrentPosition != -1) {
// Set article based on saved instance state defined during onCreateView
updateArticleView(mCurrentPosition);
}
}
public void updateArticleView(int position) {
if(position==0){
ShowTable();
}
else if(position==1)
{
ShowImg();
}
Log.e(DLL_ARG_POSITION,Integer.toString(position));
mCurrentPosition = position;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save the current article selection in case we need to recreate the fragment
outState.putInt(DLL_ARG_POSITION, mCurrentPosition);
}
public void ShowTable() {
...
}
public void ShowImg() {
...
}
}
Logcat
求教! 为什么这样传递数据的时候会出错,或者有什么解决办法吗?
此外,我需要保留 articleFrag.updateArticleView(position);的调用