Java 谷歌地图:从不同类创建标记
在我的android studio项目中,我有两个活动:一个是MapsActivity,另一个只是basic 当我点击地图时,另一个活动启动,它由一组EditText(文本字段)组成,允许用户定义标记的自定义属性。然而,当我试图从该类创建标记时,AndroidStudio给了我一个错误 以下是代码MapsActivity.java:Java 谷歌地图:从不同类创建标记,java,android,google-maps,google-maps-api-3,Java,Android,Google Maps,Google Maps Api 3,在我的android studio项目中,我有两个活动:一个是MapsActivity,另一个只是basic 当我点击地图时,另一个活动启动,它由一组EditText(文本字段)组成,允许用户定义标记的自定义属性。然而,当我试图从该类创建标记时,AndroidStudio给了我一个错误 以下是代码MapsActivity.java: package com.geekybrackets.virtualtourguide; import android.content.Intent; import
package com.geekybrackets.virtualtourguide;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public GoogleMap mMap;
double lat = 0;
double lon = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
public void onMapClick(LatLng latLng) {
lat = latLng.latitude;
lon = latLng.longitude;
startActivity(new Intent(MapsActivity.this, newMarker.class));
}
});
}
}
NewMarker.java(用户输入标记属性的活动)
这是我得到的错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
at com.geekybrackets.virtualtourguide.newMarker$2.onClick(newMarker.java:42)
我怎样才能解决这个问题?如果您有任何意见,我们将不胜感激:)哦,孩子,您还有几件事要学 首先,您不应该调用
活动
的构造函数-其思想是框架创建活动
的实例,并且您在onCreate(Bundle savedInstanceState)
上处理初始化逻辑。如果只调用构造函数,则创建的随机对象甚至不会成为UI的一部分,更不用说要使用的实例IfMapsActivity
其次,newMarker
是一个可怕的类名,您应该遵守Java惯例,以大写字母开头类名,类似于NewMarkerActivity
第三,如果要从另一个活动
更新MapsActivity
,则需要启动第二个活动以获得第一个活动的结果。基本上,您应该调用startActivityForResult(newintent(MapsActivity.this,NewMarkerActivity.class))
而不是当前调用,并在MapsActivity
中覆盖onActivityResult
。
将标题作为额外内容传递到结果包中
,并在活动结果中检索它
编辑:
要传递标题,您需要创建一个Intent
,并通过调用传递标题。之后
用那个意图打电话
。在onActivityResult的中,您可以通过调用
将该键设置为NewMarkerActivity
类的public static final
字段是一个好主意,这样您就不会打字了。哦,天哪,您还有一些东西要学
首先,您不应该调用活动
的构造函数-其思想是框架创建活动
的实例,并且您在onCreate(Bundle savedInstanceState)
上处理初始化逻辑。如果只调用构造函数,则创建的随机对象甚至不会成为UI的一部分,更不用说要使用的实例IfMapsActivity
其次,newMarker
是一个可怕的类名,您应该遵守Java惯例,以大写字母开头类名,类似于NewMarkerActivity
第三,如果要从另一个活动
更新MapsActivity
,则需要启动第二个活动以获得第一个活动的结果。基本上,您应该调用startActivityForResult(newintent(MapsActivity.this,NewMarkerActivity.class))
而不是当前调用,并在MapsActivity
中覆盖onActivityResult
。
将标题作为额外内容传递到结果包中
,并在活动结果中检索它
编辑:
要传递标题,您需要创建一个Intent
,并通过调用传递标题。之后
用那个意图打电话
。在onActivityResult的中,您可以通过调用
将该键设置为NewMarkerActivity
类的publicstaticfinal
字段是一个好主意,这样您就不会输入错误。这样您就无法获得map实例。您正在创建mMap字段设置为null的MapsActivity
对象的新实例。您必须从newMarker
via中的EditText
返回值。请参阅此处的详细信息:您无法通过这种方式获取map实例。您正在创建mMap字段设置为null的MapsActivity
对象的新实例。您必须从newMarker
via中的EditText
返回值。更多信息请参见此处:@npace速度更快,他不怕批评,所以我应该参考他的回复。@npace速度更快,他不怕批评,所以我应该参考他的回复。非常感谢您的详细回复。当收到结果时,我得到了一个意图,但是我不确定如何将“标题”作为额外的信息传递。文档似乎也没什么帮助,没问题。查看编辑,看看如何传递标题。啊,是的,听起来不错,太好了!再次感谢您的帮助,先生。非常感谢您的详细回复。当收到结果时,我得到了一个意图,但是我不确定如何将“标题”作为额外的信息传递。文档似乎也没什么帮助,没问题。查看编辑,看看如何传递标题。啊,是的,听起来不错,太好了!再次感谢您的帮助,先生。
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
at com.geekybrackets.virtualtourguide.newMarker$2.onClick(newMarker.java:42)