Moin Hashmi
Moin Hashmi

Reputation: 61

Receiving null value on sending data from Activity to the fragment withing that Activity?

Here is my code of first fragment that is sending Data to the Activity, It is working:

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            Log.i(TAG,"Place:"+place.getLatLng());
            LatLng latLng = place.getLatLng();
            double latitude = latLng.latitude;
            double longitude = latLng.longitude;
            Intent intent= new Intent(getApplicationContext(),Activity_home.class);
            intent.putExtra("latitude",latitude);
            intent.putExtra("longitude",longitude);
            startActivity(intent);
        }

        @Override
        public void onError(Status status) {
            Log.i(TAG,"An error occured:" + status);
        }
    });

Here is the Activity that is receiving the data from fragment and then sending it to 2nd fragment within it:

public class Activity_home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    Toolbar toolbar;
    FloatingActionButton floatingActionButton;
    DrawerLayout drawerLayout;
    NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);



        toolbar = (Toolbar)findViewById(R.id.toolbar);
        //floatingActionButton= (FloatingActionButton)findViewById(R.id.fab);
        navigationView = (NavigationView)findViewById(R.id.nav_view);
        drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);

        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("My Test App");


        /***************HEADER LAYOUT contents*****************/

        View nav_view = navigationView.getHeaderView(0);

        ImageView profile_img = (ImageView)nav_view.findViewById(R.id.imgProfile_drawer);
        TextView title = (TextView)nav_view.findViewById(R.id.textView_navHeader_name);
        TextView email = (TextView)nav_view.findViewById(R.id.textView_navHeader_email);

        profile_img.setImageResource(R.drawable.ic_profile_reg);
        title.setText("Moin Hashmi");
        email.setText("moin.hashmi20@gmail.com");

        /***************NAVIGATION DRAWER**********************/

       /* floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "Snackbar", Snackbar.LENGTH_LONG).show();
            }
        });*/

        ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_open,R.string.navigation_close);
        drawerLayout.addDrawerListener(drawerToggle);
        drawerToggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);
        //Double lonlat = (getIntent().getDoubleExtra("place",0));
        if (getIntent().getExtras() != null){
            double latitude = getIntent().getDoubleExtra("latitude",0.00);
            double longitude = getIntent().getDoubleExtra("longitude",0.00);
            Bundle bundle=new Bundle();

            bundle.putDouble("lat",latitude);
            bundle.putDouble("lon",longitude);
     //            bundle.putString("latitude", latitude+"");
    //            bundle.putString("longitude", longitude+"");


            Fragment fragment = new Map();
            fragment.setArguments(bundle);
            navigationView.getMenu().getItem(0).setChecked(true);
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().replace(R.id.content_home,fragment).commit();
        }

Here is the fragment code that is receiving data from parent Activity:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    if(getArguments()!=null) {
        longitude = this.getArguments().getDouble("lon");
        latitude = this.getArguments().getDouble("lat");
    }
    View view = inflater.inflate(R.layout.fragment_map, container, false);


    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.gmaps);
    mapFragment.getMapAsync(this);

    btn_bookNow=(Button)view.findViewById(R.id.btn_Booknow);
    btn_schedule=(Button)view.findViewById(R.id.btn_scheduleLater);

    btn_bookNow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

//              Toast.makeText(getActivity(),"clicked",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(getActivity(), MapSearch.class);
            startActivity(intent);
        }
  });

    return view;
}

Upvotes: 0

Views: 96

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191983

You need to use the same key names at both places either "lat" & "lon" OR "latitude" & "longitude".

This is what you have:

bundle.putDouble("lat",latitude);
bundle.putDouble("lon",longitude);

Check again

if(getArguments()!=null) {
    longitude = this.getArguments().getDouble("longitude");
    latitude = this.getArguments().getDouble("latitude");
}

I'd also suggest not putting a MapFragment inside another Fragment, but that is a separate issue

Upvotes: 1

Related Questions