【问题标题】:Passing parameters from Android FragmentActivity to Fragment将参数从 Android FragmentActivity 传递给 Fragment
【发布时间】:2012-10-04 03:05:46
【问题描述】:

当我尝试将参数从 FragmentActivity 传递给 Fragment 时,它会在 Fragment 的 getArguments() 中出现空指针异常。

这是我的 FragmentActivity 代码

  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 的示例代码。但还是没有运气

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

提前致谢

【问题讨论】:

    标签: android parameters fragment android-fragmentactivity


    【解决方案1】:
    1. 从所有不继承自Activity 的类的名称中去掉Activity。例如, IndexFragmentActivity 不是 Activity

    2. 在实际活动的onCreate() 中,您调用findFragmentById() 来检索片段,然后在该片段上调用newInstance()。但是,第一次调用onCreate() 时,该片段将不存在,您将在此处失败并返回NullPointerException。请正确处理此案。

    3. Java 中的方法名称newInstance 最常与factory pattern 相关联,其中newInstance() 是一个static 方法,用于代替公共构造函数来创建某些类的实例。您的 newInstance() 方法不是 static。这会让那些追随您维护代码的人感到困惑。

    4. 你在onCreate()中调用newInstance(),创建新的fragment实例,然后扔掉,浪费时间。

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

    当我尝试将参数从 FragmentActivity 传递到 Fragment 时

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

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

    【讨论】:

    • 非常感谢您的指导。解决问题后,我会继续工作并更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    相关资源
    最近更新 更多