我正在尝试在片段中设置Google地图并遇到一些问题,试图让按钮正确收听。我有以下代码:
Array ( [update_slider] => Update Slider [DSCF3727_jpg] => 0 [DSCF3687_jpg] => 0 [DSCF3744_jpg] => 0 [DSCF4070_jpg] => 0 [DSCF4071_jpg] => 0 [DSCF4073_jpg] => 0 [10245379_229236400605097_7329379469957650735_n_jpg] => 0 [10314007_229236253938445_8056225029697636541_n_jpg] => 0 [DSCF3618_jpg] => 0 [10550825_261238134071590_1715454340087601479_n_jpg] => 0 [1551739_261238150738255_2482363522486143465_n_jpg] => 0 [10626882_284760908385979_3153917771920003400_n_jpg] => 0 [10689730_284760921719311_8515870397004989019_n_jpg] => 0 [DSCF3384_jpg] => 0 [DSCF3382_jpg] => 0 [biker_jpg] => 0 [10410927_255245578004179_6557207701489592736_n_jpg] => 0 [10400847_255245548004182_5319163275713658480_n_jpg] => 0 [111111_jpg] => 0 [DSCF3013_jpg] => 0 [DSCF4040_jpg] => 0 [1011255_238047056390698_6002378516798338793_n_jpg] => 0 [10373785_238048733057197_8255990183542589096_n_jpg] => 0 [10383572_238047003057370_1041466926744898970_n_jpg] => 0 [DSCF4047_jpg] => 0 [DSCF4051_jpg] => 0 [DSCF4109_jpg] => 0 [DSCF4121_jpg] => 0 [DSCF4136_jpg] => 0 [DSCF4029_jpg] => 0 [Rancho-las-lomas-wedding-DJ_jpg] => 0 [webtest_jpg] => 0 ) DSCF3727_jpg
map_fragment.xml的相关部分:
public DefaultMap extends android,.support.v4.app.Fragment implements OnMapReadyCallback {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.map_fragment, container, false);
MapsInitializer.initialize(getActivity());
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
mMapFragment = new SupportMapFragment() {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mMap = mMapFragment.getMap();
if (mMap != null) {
}
}
};
mMapFragment.getMapAsync(this);
// mAutocompleteView = (AutoCompleteTextView) findViewById(R.id.TFSearch);
// mAutocompleteView.setAdapter(new PlaceAutocompleteAdapter(this, android.R.layout.simple_dropdown_item_1line));
searchSetup(v);
zoomSetup(v);
setupMapType(v);
return v;
}
//an example of one of the setup functions
public void setupMapType(View view) {
typeMapButton = (Button) view.findViewById(R.id.TFtype);
typeMapButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mMap.getMapType() == GoogleMap.MAP_TYPE_NORMAL) {
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
} else {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}
});
}
}
单击按钮时我收到以下错误(所有4个按钮都相同):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".DefaultMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/map_type"
android:id="@+id/TFtype"
android:onClick="onTypeMap"
tools:context=".DefaultMap"
android:layout_marginRight="5dp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
有人知道如何解决这些问题吗?
答案 0 :(得分:1)
使用SupportMapFragment.newInstance()
获取SupportMapFragment
的新实例,以便从Fragment中添加它,并从android:name="com.google.android.gms.maps.SupportMapFragment"
片段的xml中删除DefaultMap
。
有关详细信息,请参阅以下教程: