Android的fragment无法获取来自Activity的bundle数据


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);的调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值