[英]Google Maps: Create marker from different class
在我的android studio项目中,我有两个活动:一个是MapsActivity,另一个是基本的。
当我单击地图时,将启动另一个活动,该活动由一组EditText(文本字段)组成,允许用户为标记定义自定义属性。 但是,当我尝试从该类创建标记时,Android Studio给我一个错误。
这是代码MapsActivity.java:
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(用户输入标记属性的活动)
package com.geekybrackets.virtualtourguide;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class newMarker extends AppCompatActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_marker);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button save_btn = (Button)findViewById(R.id.btn_save);
save_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText editName = (EditText)findViewById(R.id.editName);
String marker_title = editName.getText().toString();
MapsActivity i = new MapsActivity();
i.mMap.addMarker(new MarkerOptions()
.position(new LatLng(i.lat, i.lon))
.title(marker_title));
i.mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(i.lat, i.lon)));
finish();
}
});
}
}
这是我得到的错误:
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)
我该如何解决这个问题? 任何输入将不胜感激:)
哦,男孩,您需要学习一些知识。
首先,永远不要调用Activity
的构造函数-这个想法是框架创建了Activity
的实例,并且您处理onCreate(Bundle savedInstanceState)
初始化逻辑。 如果仅调用构造函数,则会创建一个随机对象,该对象甚至都不会成为用户界面的一部分,更不用说要使用的MapsActivity
实例了。
其次, newMarker
是一个可怕的类名,您应遵守Java约定,并以大写字母开头类名,例如NewMarkerActivity
,
第三,如果要从另一个Activity
更新MapsActivity
,则需要从第二个Activity
开始,以获取第一个Activity
的结果 。 基本上,您应该调用startActivityForResult(new Intent(MapsActivity.this, NewMarkerActivity.class))
而不是当前调用,并在MapsActivity
重写onActivityResult
。 将标题作为额外的标题传递到结果Bundle
,并在onActivityResult
检索。
编辑:要传递标题,您需要创建一个Intent
并通过调用Intent.putExtra("someKey", theTitle)
传递标题。 然后,使用该Intent
调用Activity.setResult(int, Intent)
。 在onActivityResult
,可以通过调用getStringExtra("someKey")
获得标题。
最好将该键设置为NewMarkerActivity
类的public static final
字段,以免造成输入错误。
您无法通过这种方式获取地图实例。 您正在创建mMap字段设置为null的MapsActivity
对象的新实例。 您必须通过setResult(int,Intent)从newMarker
EditText
返回值。 在此处查看更多信息: 从活动中获取结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.