Übergabe von Parametern von Android FragmentActivity Fragment

Wenn Im Versuch ein parameter übergeben werden, von FragmentActivity, um ein Fragment, es gibt mir null-Zeiger-Ausnahme in der getArguments() in das Fragment.

Hier ist mein 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");

        }

    }

Hier ist die 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>

und Hier ist das Fragment

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;
    }
}

und Hier ist mein FEHLER

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

Bezog ich mich viel stackoverflow relevanten Fragen zu diesem und versuchte die Probe code gegeben http://developer.android.com/guide/components/fragments.html als gut. Aber immer noch kein Glück

Wirklich zu schätzen jede feed-back auf dieses, da dies zu sein scheint eine sehr einfache Frage, und ich kann nicht herausfinden..

Vielen Dank im Voraus

InformationsquelleAutor virtualpathum | 2012-10-13

Schreibe einen Kommentar