Here is my app through which i'm trying to achieve an aim of map being taken to a location whenever an item is clicked on the navigation bar....But here i am encountering with this Class Cast Exception...I've tried to fix it..but to no avail

MY MAIN ACTIVITY::::

public class MainActivity extends Activity {

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
CustomDrawerAdapter adapter;
private  GoogleMap map;
List<DrawerItem> dataList;



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

    // Initializing
    dataList = new ArrayList<DrawerItem>();
    mTitle = mDrawerTitle = getTitle();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);

    String name = "medal_tally";

    int medal_tally = getResources().getIdentifier(name , "drawable", getPackageName());



    // Add Drawer Item to dataList
    dataList.add(new DrawerItem("Medal Tally", medal_tally,"35.689506,139.6917"));
    dataList.add(new DrawerItem("Games Schedule", R.drawable.schedule,"28.613889,77.208889"));
    dataList.add(new DrawerItem("Venues", R.drawable.venue,"52.516667,13.383333"));

    dataList.add(new DrawerItem("Participating Nations' List", R.drawable.nations,"48.8567,2.3508"));
    dataList.add(new DrawerItem("News Feeds", R.drawable.news,"39.933333,32.866667"));
    dataList.add(new DrawerItem("About App", R.drawable.about,"35.696111,51.423056"));
    adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
            dataList);

    mDrawerList.setAdapter(adapter);

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        SelectItem(0);
    }

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void SelectItem(int possition) {

    Fragment fragment = null;
    Bundle args = new Bundle();

    fragment = new MAPFragment();




Log.e("In main activity","Lets see what happens");



String[] coords = dataList.get(possition).getGeo().split(",");
Double c1 = new Double(Double.valueOf(coords[0]));
Double c2 = new Double(Double.valueOf(coords[1]));


    fragment.setArguments(args);



    FragmentManager frgManager = getFragmentManager();
    final Fragment existingFragment = frgManager.findFragmentById(R.id.map);


    if(existingFragment !=null){

         Log.e("existingFragment !=null","Lets see what happens");
         try{
        MAPFragment mapFragment = (MAPFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.setParameters(c1,c2);

         }
         catch(ClassCastException e){
             e.printStackTrace();
             Toast.makeText(getApplicationContext(), "ClassCastException...HELPME :/", Toast.LENGTH_SHORT).show();
             Log.e("inCatch","ClassCastException");
         }
    }
    else
     frgManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    mDrawerList.setItemChecked(possition, true);
    setTitle(dataList.get(possition).getItemName());
    mDrawerLayout.closeDrawer(mDrawerList);

}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem item= menu.findItem(R.id.action_settings);
    item.setVisible(false);
    return true;
}
@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggles
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // The action bar home/up action should open or close the drawer.
    // ActionBarDrawerToggle will take care of this.
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return false;
}

private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        SelectItem(position);

    }
}

}

Here's my map fragment:::::::

public class MAPFragment extends MapFragment implements receiveData {

public  String IMAGE_RESOURCE_ID = "iconResourceID";
public  String ITEM_NAME = "itemName";
public  String GEO = "39.933333,32.866667";

public void setParameters(double lat, double lon){
    navigateToNewLocation(lat, lon);
}
// Google Map
private GoogleMap googleMap;
  ImageView ivIcon;
  TextView tvItemName;
  MapView mapView;


  String[] coords = GEO.split(",");
  Double c1 = new Double(Double.valueOf(coords[0]));
  Double c2 = new Double(Double.valueOf(coords[1]));

  public MAPFragment() {

  }

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

        View view = inflater.inflate(R.layout.fragment_layout_one, container,
                    false);
        Log.e("coords[0]",coords[0]);
        /*
        ivIcon = (ImageView) view.findViewById(R.id.frag1_icon);
        tvItemName = (TextView) view.findViewById(R.id.frag1_text);

        tvItemName.setText(getArguments().getString(ITEM_NAME));
        ivIcon.setImageDrawable(view.getResources().getDrawable(
                    getArguments().getInt(IMAGE_RESOURCE_ID)));
                    */
        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return view;
  }


 /**
   * function to load map. If map is not created it will create it for you
   * */
  public void initilizeMap() {
      if (googleMap==null){
        googleMap=((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        googleMap.getUiSettings().setRotateGesturesEnabled(true);
        googleMap.setMyLocationEnabled(true);

        Log.e("In MAPFragment","Before coords!=null");


        googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(c1,c2)));





          LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
          Criteria criteria = new Criteria();
          String provider = locationManager.getBestProvider(criteria, true);
          Location location = locationManager.getLastKnownLocation(provider);

          if(location!=null){
              onLocationChanged(location);
          }
          //following line checks location of user !
          //locationManager.requestLocationUpdates(provider, 2000, 0,this);

          googleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

              @Override
              public void onMyLocationChange(Location arg0) {
                  // TODO Auto-generated method stub


                 // mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("im here ;)"));
              }



          });




        //checking
        if(googleMap==null){
            Toast.makeText(getActivity().getApplicationContext(), "Unable to create", Toast.LENGTH_SHORT).show();
        }
    }
  }

  @Override
  public void onResume() {
      super.onResume();
      initilizeMap();
  }

  public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
     // Getting latitude of the current location
      double latitude = location.getLatitude();

      // Getting longitude of the current location
      double longitude = location.getLongitude();

      // Creating a LatLng object for the current location
      LatLng latLng = new LatLng(latitude, longitude);

      // Showing the current location in Google Map
      googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

      // Zoom in the Google Map
      googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

}

  @Override
  public void onDestroyView()
  {
      super.onDestroy();
    mapView.onDestroy();
      }

  public void navigateToNewLocation(double lat, double lon){

      Log.e("INSIDE navigateToNewLoc","next is cam pos");
        // On clicking a user       
        CameraPosition cameraPosition = new CameraPosition.Builder().target(
                new LatLng(lat,lon)).zoom(12).build();
         Log.e("INSIDE navigateToNewLoc","next is animateCam");
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    // create marker
        //MarkerOptions marker = new MarkerOptions().position(new LatLng(c1,c2)).title(ITEM_NAME);

        // Changing marker icon
        //marker.icon(BitmapDescriptorFactory.fromResource(IMAGE_RESOURCE_ID));

        // adding marker
        //googleMap.addMarker(marker);
  }
}

Related posts

Recent Viewed