Google Maps API 设置 Button Click Listeners 错误
Google Maps API setting Button Click Listeners wrong
我正在尝试在片段中设置 Google 地图,并且 运行 遇到了一些问题,试图让按钮正确收听。我有以下代码:
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);
}
}
});
}
}
map_fragment.xml的相关作品:
<?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>
点击按钮时出现以下错误(所有 4 个按钮都相同):
11-15 22:57:22.481 32523-32523/oose2017.place2b E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.google.android.gms.maps.GoogleMap.getMapType()' on a null object reference
11-15 22:57:22.481 32523-32523/myproject E/AndroidRuntime: at myproject.interfaces.map.DefaultMap.onClick(DefaultMap.java:153)
有人知道我该如何解决这些问题吗?
使用 SupportMapFragment.newInstance()
获取 SupportMapFragment
的新实例,以便从片段中添加它,并从 DefaultMap
片段的 xml 中删除 android:name="com.google.android.gms.maps.SupportMapFragment"
。
有关详细信息,请参阅以下教程:
我正在尝试在片段中设置 Google 地图,并且 运行 遇到了一些问题,试图让按钮正确收听。我有以下代码:
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);
}
}
});
}
}
map_fragment.xml的相关作品:
<?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>
点击按钮时出现以下错误(所有 4 个按钮都相同):
11-15 22:57:22.481 32523-32523/oose2017.place2b E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.google.android.gms.maps.GoogleMap.getMapType()' on a null object reference
11-15 22:57:22.481 32523-32523/myproject E/AndroidRuntime: at myproject.interfaces.map.DefaultMap.onClick(DefaultMap.java:153)
有人知道我该如何解决这些问题吗?
使用 SupportMapFragment.newInstance()
获取 SupportMapFragment
的新实例,以便从片段中添加它,并从 DefaultMap
片段的 xml 中删除 android:name="com.google.android.gms.maps.SupportMapFragment"
。
有关详细信息,请参阅以下教程: