Fragment里面的MapView onCreate崩溃

时间:2017-03-23 08:52:32

标签: android android-fragments crash fragment android-mapview

我已经尝试过很多方法让它工作但是每次我遇到MapView oncreate命令崩溃的问题。我不知道我做错了什么......

片段执行:

public partial class Form1 : Form
{
    //variable for "remembering" subItem on which tooltip is shown
    ListViewItem.ListViewSubItem selectedListViewSubItem = new ListViewItem.ListViewSubItem();

    public Form1()
    {
        InitializeComponent();

    } 

    private void Form1_Load(object sender, EventArgs e)
    {
        //subscribe to events - when mouse leaves listview and when mouse is moved over listview
        listView1.MouseLeave += ListView1_MouseLeave;
        listView1.MouseMove += ListView1_MouseMove;

    }

    private void ListView1_MouseMove(object sender, MouseEventArgs e)
    {
        //using current location, check what is under mouse
        var info = listView1.HitTest(e.Location);

        //if any subitem is selected AND subitem differs from last one
        if (info.SubItem != null  && info.SubItem != selectedListViewSubItem)
        {
            //remember current subitem
            selectedListViewSubItem = info.SubItem;
            //set tooltip's new location to the bottom right of mouse pointer
            Point calculatedLocation = new Point(e.X + 20, e.Y + 20);
            //show tooltip - with subitem text, on this window, on calculated location and hold it there for 3 seconds.
            toolTip1.Show(info.SubItem.Text, listView1, calculatedLocation, 3000);
        }
    }

    private void ListView1_MouseLeave(object sender, EventArgs e)
    {
        //hide tooltip
        this.toolTip1.Hide(this);
    }

片段类:

ToolTip

使用MapView的XML:

FragmentManager sFm = getSupportFragmentManager();
        sFm.beginTransaction().replace(R.id.content_main, new MapHandler()).commit();

logcat的:

public class MapHandler extends Fragment implements OnMapReadyCallback{

MapView mapView;
GoogleMap map;

@Override public void onDetach() {
    super.onDetach();
}

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

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

       View v = inflater.inflate(R.layout.activity_maps, container, false);
    switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) )
    {
        case ConnectionResult.SUCCESS:
            Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
            mapView = new MapView(getContext());
            mapView = (MapView) v.findViewById(R.id.map);
            mapView.onCreate(savedInstanceState);
            // Gets to GoogleMap from the MapView and does initialization stuff
            if(mapView!=null)
            {
                mapView.getMapAsync(this);
            }
            break;
        case ConnectionResult.SERVICE_MISSING:
            Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
            break;
        case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
            Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
            break;
        default: Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
    }

    return v;
}

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

@Override public void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState);
}
@Override public void onLowMemory()
{
    super.onLowMemory();
    mapView.onLowMemory();
}
@Override public void onResume() {
    super.onResume();
    mapView.onResume();
}
@Override public void onDestroyView() {
    super.onDestroyView();
}
@Override public void onMapReady(GoogleMap googleMap)
    {

    }}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我发现google maps api key在我的AndroidManifest文件中存储了两次,这就是问题所在。谢谢你的所有答案。