0

I have a problem with passing a number from one fragment to another in the same activity , I got that number to Activity, but when I try to pass it to 2nd fragment it gives me null. Any help is appreciated

Here is the code for the Passing and a bit more, there maybe typos as i wanted to put as little of code as I could

MainActivity

package com.example.design.ex;

import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import example.test.R;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.example.design.ex.fragments.ReceiptItemsFragment;


public class NewReceiptActivity extends SherlockFragmentActivity {

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


       int num = 0;

       Intent intent = getIntent();
       int Broj = intent.getIntExtra("Num", num);


     Bundle bundle=new Bundle();
    bundle.putInt("Broj", Broj);

         setContentView(R.layout.activity_test);

         ReceiptItemsFragment frag = (ReceiptItemsFragment) 
getSupportFragmentManager().findFragmentById(R.id.receipt);
         frag.setArguments(bundle);


     }


 }

Fragment 1

package com.example.design.ex.fragments;

import example.test.R;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.actionbarsherlock.app.SherlockFragment;

public class CategoriesItemsFragment extends SherlockFragment  {


int num = 0;

@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_categories_items,
             container, false);
    return view;
 }

@Override
 public void onViewCreated(View view, Bundle savedInstanceState) {
     super.onViewCreated(view, savedInstanceState);
     init();
 }

 private void init() {


     itemsGrid.setOnItemClickListener(new OnItemClickListener() 
    {
         @Override
        public void onItemClick(AdapterView<?> arg0, final View itemView, int 
 arg2, long arg3)
         {
               num = arg2 + 1;


Intent intent = new Intent(getActivity().getBaseContext(),NewReceiptActivity.class);


          intent.putExtra("Num", num);
     startActivity(new Intent(getActivity(), NewReceiptActivity.class));   
          getActivity().startActivity(intent);


        }

     });


  }
}

Fragment 2

package example.design.ex.fragments;

import example.test.R;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;




public class ReceiptItemsFragment extends SherlockFragment {

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

    setHasOptionsMenu(true);



     Bundle arguments = getArguments();
     if (arguments != null)
    {
        Log.d("ISnull","no!");
     } else {
         Log.d("ISnull","yes!");
     }


 int num=getArguments().getInt("Num");


     return inflater.inflate(R.layout.fragment_receipt_items, container,
             false);


 }

Main Activity XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >

<fragment
    android:id="@+id/titles"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="1.5"
    class="example.ex.fragments.CategoriesItemsFragment" />

<LinearLayout
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:background="@color/sun_flower" >
</LinearLayout>

<fragment
    android:id="@+id/receipt"
    android:name="example.ex.fragments.ReceiptItemsFragment"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="1"
    class="example.ex.fragments.ReceiptItemsFragment" />

</LinearLayout>

Here is the Error

 06-11 07:41:00.419: E/AndroidRuntime(2801):    at      
 com.example.NewReceiptActivity.onCreate(NewReceiptActivity.java:60)

which is this line or the next if i put log (tried to get it to show me the id)

ReceiptItemsFragment frag = (ReceiptItemsFragment)    
getSupportFragmentManager().findFragmentById(R.id.receipt); 
MePo
  • 1,044
  • 2
  • 23
  • 48

4 Answers4

1

I think the problem is here :

 ReceiptItemsFragment frag=new ReceiptItemsFragment(); //2nd fragment

 frag.setArguments(bundle);

You're creating a new fragment but its not the one you've got in your layout.

try this instead :

setContentView(R.layout.activity_test); // Here is where that 2 fragments are Located

ReceiptItemsFragment frag = getFragmentManager().findFragmentById(...) //your fragment id

frag.setArguments(bundle);

so you don't create a new fragment but reference the one you've got in your layout.

Hope that helps.

Guian
  • 4,563
  • 4
  • 34
  • 54
  • Fragment frag = getFragmentManager().findFragmentById(R.id.receipt) This gives me null... i will include the xml for that fragment – MePo Jun 11 '14 at 08:45
  • You may need to use the suport fragment manager (if your fragments use support library) , see : http://stackoverflow.com/questions/16967328/mapfragment-findfragmentbyid-always-null – Guian Jun 11 '14 at 09:10
  • The support library is better maintained than the ones in android.app therefore it's recommended to use them. – EpicPandaForce Jun 11 '14 at 09:30
  • 1
    In fact, I think Sherlock relies on the Support Library, therefore yes, you need the Support FragmentManager. – EpicPandaForce Jun 11 '14 at 09:40
  • @Zhuinden i did that but same thing I get null – MePo Jun 11 '14 at 10:34
  • Thanks Guian and @Zhuinden you helped me figure out this. – MePo Jun 12 '14 at 11:47
1

Verify if frag in NewReceiptActivity is different null. Verify if Numt is 2.

Move the line in ReceiptItemsFragment:

int num=getArguments().getInt("Num"); //this should get number 2 from Frist Fragment but it gives null

from method onCreateView to onActivityCreated:

public void onActivityCreated (Bundle savedInstanceState) {
    int num=getArguments().getInt("Num"); //this should get number 2 from Frist Fragment but it gives null
Lucas Santos
  • 597
  • 6
  • 18
  • i Did its 0 at first (or any number I put in num), later it gets changed to 2, i will try to Move that to onActivityCreated – MePo Jun 11 '14 at 11:33
  • @MePo please add more code in your question. And what's order of process your app, who is first, second, third? Who call who? – Lucas Santos Jun 11 '14 at 11:54
  • I wanna understand a little more about logic and order of call from your app. – Lucas Santos Jun 11 '14 at 12:00
  • Sure thing... its driving me crazy. – MePo Jun 11 '14 at 12:10
  • Expanded the code a bit (added important parts) for the solution that is bothering me. – MePo Jun 11 '14 at 12:48
  • Who calls NewReceiptActivity? – Lucas Santos Jun 11 '14 at 13:35
  • Button with `startActivity(new Intent(getBaseContext(),NewReceiptActivity.class));` He acts nothing more than a starting point for picking – MePo Jun 11 '14 at 13:43
  • If you start `Activity` this form: `startActivity(new Intent(getBaseContext(),NewReceiptActivity.class));` You not passing none argument for `Intent` which `NewReceiptActivity` can get by `Intent intent = getIntent(); int Broj = intent.getIntExtra("Num", num);` – Lucas Santos Jun 11 '14 at 13:55
  • I use this intend to get num from Fragment 1 to Activity – MePo Jun 12 '14 at 06:57
  • I think I got a wrong idea about fragments, I think I am using them as I would Activities. – MePo Jun 12 '14 at 07:05
  • Still is hard helps you because i don't understood your logic app. The order of execution. Who calls who. When your app starts, what's first acitivity called? Sorry for my english. – Lucas Santos Jun 12 '14 at 11:27
  • I Went on a different approach using Interface and I got it working... So I will thank you for the help. Since the problem was in me thinking about the fragments as they where activites. – MePo Jun 12 '14 at 11:37
  • @MePo You could solve your problem? If so, put yourself an answer with the solution and accept your own answer. – Lucas Santos Jun 12 '14 at 11:40
  • Yes I did and I will, but still thanks for the help, you made me understand some things. – MePo Jun 12 '14 at 11:45
0

You can create a subclass of Application and store the object there. Check this. Or you can use static variables in the activity class.

Community
  • 1
  • 1
Gheo
  • 98
  • 6
  • I will try it this way... if I cant get it to work the way I wanted, and Thank you for the suggestion – MePo Jun 11 '14 at 08:51
0

I went on different approach using Interface on passing data,As explained here

https://stackoverflow.com/a/12311939/1393695

And got it working, I want to thank everybody here for the help.

The problem was that I was looking at fragments as they where activities.

Community
  • 1
  • 1
MePo
  • 1,044
  • 2
  • 23
  • 48