gpt4 book ai didi

java - fragment 上的 MapFragment

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:02 25 4
gpt4 key购买 nike

我正在开发一个在 fragment 上显示 map 的应用程序。一切都已准备就绪,但有一点小事情让我无法完成。

gMapFragment.java:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class gMapFragment extends Fragment implements OnMapReadyCallback {


public gMapFragment() {
// Required empty public constructor
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_map, container, false);
}

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

MapFragment fragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {

LatLng marker = new LatLng(46.33328, 15.38173);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker,13));
googleMap.addMarker(new MarkerOptions().title(("HERE WE ARE!")).position(marker));

}
}

我从另一个框架中的按钮调用这个 fragment ,这是调用它的方法:

public void onClick(View view) {
Fragment fragment = null;
switch (view.getId()) {
case R.id.SearchButton:
fragment = new gMapFragment();
replaceFragment(fragment);
break;
}
}

public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}

我收到错误“gMapFragment 无法转换为 fragment ”知道如何解决此问题吗?谢谢!

最佳答案

首先,确保您的 Fragment 导入保持一致。在所有类中使用 import android.app.Fragment 或在所有类中使用 import android.support.v4.app.Fragment; 。不要混合搭配。

然后,将其添加到 Activity 中:

public void openMapFragment() {
Fragment fragment = new gMapFragment();
replaceFragment(fragment);
}

//moved to Activity from the Fragment
public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}

然后,在 Fragment onClick() 方法中,调用 Activity 来执行 FragmentTransaction:

public void onClick(View view) {
switch (view.getId()) {
case R.id.SearchButton:
MainActivity activity = (MainActivity) getActivity();
if (activity != null) {
activity.openMapFragment();
}
break;
}
}

关于java - fragment 上的 MapFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40595860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com