getArgument es NULL, ¿por qué?

I am trying to pass values through a Bundleusando setArguments() from an activity to another class extends Fragment when I press one of the listed items in a ListView.

As shown below, I used a Log.i statement to know if the Bundle received is null or not. Unfortunately, it is always null and consequently, no data shows on the designated TextView of the class that extends Fragment. What I am missing or what is wrong with the code.

FragmentClass:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View view = inflater.inflate(R.layout.fragmentclasslayout, null);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    if (getArguments() != null) {
        Log.i(TAG, "getArgument is not null");
        int value = getArguments().getInt("pos");           
        TextView tv = (TextView) getView().findViewById(R.id.tvID);
        tv.setTextSize(value);
        tv.setText("size = "+value*10);
    }else {
        Log.i(TAG, "getArgument is null");
    }
}

MainClass_implements_onItemSelectedListener:

setContentView(R.layout.mainactivity);

    ListView mListView = (ListView) findViewById(R.id.list);
    ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
            R.layout.listviewlayout, R.id.tv, string);
    mListView.setAdapter(mArrayAdapter);
    mListView.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub

    Fragment f = new FragmentClass();
    Bundle bundle = new Bundle();

    bundle.putInt("pos", (position+1));
    FragmentClass fc = new FragmentClass().newInstance(bundle);
    //f.setArguments(bundle);

    FragmentTransaction t =
            getFragmentManager().beginTransaction();
            t.replace(R.id.fragment00ID, f);
            t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            t.addToBackStack(null);
            t.commit();
}

mainActivity_layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:ignore="MergeRootFrame">

<ListView
    android:id="@+id/list"
    android:layout_width="100px"
    android:layout_weight="1"
    android:layout_height="match_parent" />
<fragment
    android:name="com.example.fragmentwithlistview.FragmentClass"
    android:id="@+id/fragment00ID"
    android:layout_toRightOf="@+id/list"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_weight="4" />

FragmentClass_layout:

<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:ignore="MergeRootFrame"
android:background="#FFFFFF">

<TextView
    android:id="@+id/tvID"
    android:textColor="#000000"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

preguntado el 28 de mayo de 14 a las 12:05

how are you setting arguments -

@IllegalArgument please have alook at the method onitemselected' it is bundle.putInt("pos", (position+1));` is that what you mean? -

R.id.fragment00ID refers to your activity layout you want to replace right?? -

yes, i will post the layout as well shortly -

anything to do with addToBackStack(null)? -

2 Respuestas

Better use a static method for instantiating:

public class FragmentClass extends Fragment {

  public static FragmentClass newInstance(Bundle b) {
    FragmentClass fragment = new FragmentClass();
    fragment.setArguments(b);

    return fragment;
  }

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragmentclasslayout, null);

      if (getArguments() != null) {
          Log.i(TAG, "getArgument is not null");
          int value = getArguments().getInt("pos");           
          TextView tv = (TextView) view.findViewById(R.id.tvID);
          tv.setTextSize(value);
          tv.setText("size = "+value*10);
      } else {
          Log.i(TAG, "getArgument is null");
      }

      return view;
  }
}

Then simply call that method and pass the arguments-bundle as a parameter:

Bundle bundle = new Bundle();
bundle.putInt("pos", (position+1));
FragmentClass f = FragmentClass.newInstance(bundle);

Además, no use onActivityCreated for fetching the arguments in your FragmentClass, use onCreateView or if required onStart or onResume. Have a look at fragment-lifecycles: http://developer.android.com/guide/components/fragments.html#Lifecycle

contestado el 28 de mayo de 14 a las 13:05

I triied your suggestion but still no data appears? - user3558352

I tried it, the same thing again, no data output, the fragment area is aemptyt - user3558352

AFAIK, in onCreateView one can't initialise the view's using getview.finditembyid() because the view has not fully initialised yet. but inside it we can initialise objects and instantiate them. am I right? - user3558352

Why are you instantiating your fragment outside of onItemSelected? I guess that's where your problem is then, look at kevin's comment. In onCreateView you inflate your view and use findViewById en esa vista. - Blacklight

I just noticed in your edit: not new FragmentClass().newInstance, Sólo FragmentClass.newInstance. Other than that it's hard to give more advice, pay attention to how you use the FragmentManager as well as to the Fragment lifecycles. - Blacklight

You can use in passing Activity

Bundle bundle = new Bundle();
    bundle.putString("model", strDeckNumber);

                    final FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
                    Frag1 newFragment = new Frag1();
                    newFragment.setArguments(bundle);
                    t.replace(R.id.fragment_container, newFragment);
                    t.addToBackStack(TAG);
                    t.commit();

Receiving Fragment

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

            strSeriesNumber = getArguments().getString("model");
}

contestado el 28 de mayo de 14 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.