Jon Hudson
Jon Hudson

Reputation: 17

setArgument not working in passing data in fragment

I want to pass data between two fragment,

Here is my setArgument

ListFragment fragment = new ListFragment();
Bundle bundle = new Bundle();

bundle.putString("Test", "test");
fragment.setArguments(bundle);

And here is my getArgument

In my onCreate method

String test = args.getString("Test","test");

if(this.getArguments()!=null)
{
    Log.d("hey", "hey");
    test = this.getArguments().getString("Test","test");
    details_id.setText(test);
}

I have java.lang.NullPointerException for details_id.setText(test);

My get Argument is working I can get hey: hey in my log but my setArgument is not working.

UPDATE

I don't have any error now but I cannot pass any argument

Here is my updated onCreate method

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    chooseTrip(tripIds.get(position));

    String selectedTripId = tripIds.get(position);

    Helpers.saveToSharedPreferences(getActivity(),
            Constants_Prefs.SELECTED_TOP_LEVEL_RECORD,
            Constants_Keys.SELECTED_TRIP_ID,
            selectedTripId);

    ListFragment fragment = new ListFragment();
    Bundle bundle = new Bundle();

    bundle.putString("Test", "test");
    fragment.setArguments(bundle);
}

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

    final View fragV = inflater.inflate(R.layout.fragment_main, container, false);
    listView = (ListView)fragV.findViewById(R.id.listView);
    listView.setOnItemClickListener(this);

    TextView details_id = (TextView)fragV.findViewById(R.id.detail_header_id);
    details_id.setVisibility(View.VISIBLE);
    if(this.getArguments()!=null){
        Log.d("hey", "hey");
        test = this.getArguments().getString("Test","t");
        details_id.setText(test);
    }
}

My Log: hey﹕ hey

My textView print: if I use test = this.getArguments().getString("Test","t"); -> my textView is t

If I use test = this.getArguments().getString("Test"); -> my textView is nothing

Upvotes: 0

Views: 2468

Answers (1)

stoyan
stoyan

Reputation: 166

Its not your getArguments() that is not working. You are calling details_id.setText(test) in onCreate(Bundle savedInstanceState). I am assuming that details_id is a TextView?

You have to initialize the views of a fragment in its onCreateView method, which is called after onCreate by the system

==========EDIT=======

Try this in your fragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View fragV = inflater.inflate(R.layout.fragment_main, container, false);
    listView = (ListView)fragV.findViewById(R.id.listView);
    listView.setOnItemClickListener(this);


    TextView details_id = (TextView)fragV.findViewById(R.id.detail_header_id);
    details_id.setVisibility(View.VISIBLE);
    if(this.getArguments()!=null){
        Log.d("hey", "hey");
        test = this.getArguments().getString("Test","t");
        details_id.setText(test);
    }
    return fragV;
}

Try this in your activity that creates the fragments:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    ListFragment fragment = new ListFragment();
    Bundle bundle = new Bundle();

    bundle.putString("Test", "test");
    fragment.setArguments(bundle);
    showFragment(fragment, true);
}

private void showFragment(Fragment frag, boolean addToBackStack) {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();


    transaction.replace(R.id.content_frame, frag);
    if (addToBackStack) {
        transaction.addToBackStack(null);
    }
    transaction.commit();
}

And your main_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
<LinearLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

Upvotes: 1

Related Questions