Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kangaroo Rewards
Courses Plus Student 5,699 PointsMapFragment
I have a fragment. Inside of this one I have:
fragment android:id="@+id/map_branch" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/layout_bottom" class="com.google.android.gms.maps.MapFragment" />
In
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_stores, container);
It gives me the next bug:
Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
I found many answer using SupportFragment, but I want to use MapFragment.
Thanks.
2 Answers
Ben Jakuben
Treehouse TeacherCan you paste in all your code, including the import statements?
Kangaroo Rewards
Courses Plus Student 5,699 PointsSorry for answer you so late. I would like to describe my target in the application. I need to implement a listview vertical or horizontal with many google maps controls. This listview will be customize with an ArrayAdapter and a specific layout.
MainActivity.java
package com.maps123;
import java.util.ArrayList;
import java.util.List;
import com.maps123.HorizontalListView;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
GMapV2Direction mapDirection;
private HorizontalListView hlvMapList;
public PlaceholderFragment() { }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
hlvMapList = (HorizontalListView) rootView.findViewById(R.id.hlvMapList);
List<Direction> list = new ArrayList<Direction>();
list.add(new Direction("45.496290400000", "-73.555660200000"));
list.add(new Direction("47.509036000000", "-67.639025500000"));
MapListAdapter adapterMapBraches = new MapListAdapter(getActivity(), R.layout.row_maps_hlist, list);
hlvMapList.setAdapter(adapterMapBraches);
return rootView;
}
public class Direction {
String latitude, longitude;
public Direction() { }
public Direction(String latitude, String longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
}
}
}
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.maps123.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.maps123.MainActivity$PlaceholderFragment" >
<com.maps123.HorizontalListView
android:id="@+id/hlvMapList"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginTop="4dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp" />
</RelativeLayout>
row_maps_hlist.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="170dp"
android:layout_height="160dp" >
<fragment
android:id="@+id/map_branches"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
class="com.google.android.gms.maps.MapFragment" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:visibility="invisible" >
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Simple String List"
android:textStyle="bold"
android:textSize="13sp" />
<TextView
android:id="@+id/tvDescription"
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center_horizontal"
android:text="Simple String List /n asdkfjasdkfjaskdfj"
android:ellipsize="end"
android:textSize="10sp" />
</LinearLayout>
</RelativeLayout>
Thanks for your help.
Ben Jakuben
Treehouse TeacherIt looks like you're using PlaceholderFragment, which extends the Fragment class, but your layout is expecting a MapFragment instead. What happens if you change PlaceholderFragment to extend MapFragment instead?