将参数从Android FragmentActivity传递到Fragment

当我试图将一个参数从FragmentActivity传递给Fragment时,它在Fragment的getArguments()中给出了空指针异常.

这是我的FragmentActivity Code

  public class IndexChartActivity extends FragmentActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.index_chart);

            IndexFragmentActivity indexFragment =   
 (IndexFragmentActivity)getSupportFragmentManager().findFragmentById(R.id.index_fragment);
            indexFragment.newInstance("ASPI");

        }

    }

这是index_chart.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/header_fragment"
        android:name="com.lk.ignitionit.cse.util.HeaderFragmentActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <fragment
        android:id="@+id/index_fragment"
        android:name="com.lk.ignitionit.cse.util.IndexFragmentActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <fragment
        android:name="com.lk.ignitionit.cse.util.ChartFragmentActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

这是碎片

public class IndexFragmentActivity extends Fragment {
    protected ImageView ivASPI;
    protected ImageView ivMPI;
    protected ImageView ivSP;
    protected TextView tvMain;
    protected TextView tvTop;
    protected TextView tvBottom;
    String response = null;
    String result = null;
    String []  resultArr = null;
    Bundle b = new Bundle();
    String indexType = null;
    int layout;
    IndexFragmentActivity f = null;

    public  IndexFragmentActivity newInstance(String index) {
        f = new IndexFragmentActivity();
        Bundle args = new Bundle();
        args.putString("indextype", index);
        f.setArguments(args);
        return f;
    }

    public String getSelectedIndex() {
        return f.getArguments().getString("indextype");
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(getSelectedIndex().equals("ASPI")){
            layout = R.layout.aspi_header;
        }else if(getSelectedIndex().equals("MPI")){
            layout = R.layout.mpi_header;
        }else{
            layout = R.layout.sp_header;
        }
        View view = inflater.inflate(layout, container, false);

        tvMain = (TextView) view.findViewById(R.id.tv_main);
        tvTop = (TextView) view.findViewById(R.id.tv_top);
        tvBottom = (TextView) view.findViewById(R.id.tv_bottom);


        new ServiceAccess().execute("");

        tvMain.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                b.putString("type", "S&P SL20");
                Activity activity = getActivity();
                Intent intent = new Intent(activity, IndexChartActivity.class);
                intent.putExtras(b);
                startActivity(intent);  

            }
        });

        return view;
    }
}

这是我的错误

Caused by: java.lang.NullPointerException
at com.lk.ignitionit.cse.util.IndexFragmentActivity.getSelectedIndex(IndexFragmentActivity.java:69)

我提到了很多与此相关的stackoverflow问题,并尝试了给出http://developer.android.com/guide/components/fragments.html的示例代码.但仍然没有运气

真的很感激任何反馈,因为这似乎是一个非常基本的问题,我无法弄清楚..

提前致谢

最佳答案
>从不从Activity继承的所有类的名称中删除Activity.例如,
IndexFragmentActivity不是Activity.
>在实际活动的onCreate()中,您调用findFragmentById()来检索片段,然后在该片段上调用newInstance().但是,在第一次调用onCreate()时,片段将不存在,并且您将在此处使用NullPointerException失败.请正确处理此案例.
> Java中的方法名newInstance最常与factory pattern相关联,其中newInstance()是一个静态方法,用于代替公共构造函数来创建某个类的实例.您的newInstance()方法不是静态的.对于那些来维护代码的人来说,这会引起混淆.
>您在onCreate()中调用newInstance(),创建新的片段实例,然后将其丢弃,这是浪费时间.

因此,假设您的片段的原始实例(无论它来自何处)没有Bundle set参数,这将解释getSelectedIndex()中的NullPointerException.

When Im trying to pass a parameter from FragmentActivity to a Fragment

要将参数传递给新创建的片段,使用静态newInstance()方法和参数Bundle来创建新片段是完全合理的.

要将参数传递给已存在的片段,只需在该片段上调用一些setter方法即可.

转载注明原文:将参数从Android FragmentActivity传递到Fragment - 代码日志