我想将谷歌地图显示到自定义对话框中..但我无法从对话类访问SupportFragmentManager ...
这是我的代码......
public class AddressViewDialog extends MaterialDialog.Builder implements OnMapReadyCallback {
private MaterialDialog reviewDialog;
private double latitude = 23.0;
private double longitude = 90.0;
private SupportMapFragment mMapFragment;
private GoogleMap mMap;
public AddressViewDialog(Context context) {
super(context);
this.context = context;
initializeView();
}
public void initializeView() {
reviewDialog = new MaterialDialog.Builder(this.getContext())
.title(R.string.request_to_introduce)
.customView(R.layout.dialog_pending_introducer_review, true)
.show();
View v = reviewDialog.getCustomView();
mMapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.fragment);
mMapFragment.getView().setVisibility(View.GONE);
if (latitude != 0.0 && longitude != 0.0) {
mMapFragment.getView().setVisibility(View.VISIBLE);
mMapFragment.getMapAsync(this);
} else
mMapFragment.getView().setVisibility(View.GONE);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng currentLoc = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(currentLoc).title("My Location"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLoc, 14.0f));
}
}
这是我的xml ..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_margin="@dimen/activity_horizontal_margin">
<fragment
android:id="@+id/fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="250dp" />
</LinearLayout>
它显示此行中的错误: mMapFragment =(SupportMapFragment)this.getChildFragmentManager() .findFragmentById(R.id.fragment);
我也试过: mMapFragment =(SupportMapFragment)this.getSupportFragmentManager() .findFragmentById(R.id.fragment);
答案 0 :(得分:0)
您应该致电getSupportFragmentManager()
而不是MaterialDialog.Builder
对象(您的AddressViewDialog
扩展MaterialDialog.Builder
),而不是AppCompatActivity
个对象。所以使用
mMapFragment = (SupportMapFragment) ((AppCompatActivity) this.context).getSupportFragmentManager()
.findFragmentById(R.id.fragment);
而不是
mMapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.fragment);
以这种方式从AddressViewDialog
创建MainActivity
:
new AddressViewDialog(MainActivity.this).show();
(MainActivity
应扩展AppCompatActivity
)。