基本的に私のアプリケーションにマップを表示するためにmapviewを使用しています。ほとんどのマップアプリケーションのようにフラグメントを使用しないことを意味します。しかし、私はマップにマーカーを追加する問題に直面している。私を助けてください。MapViewにマーカーを追加する方法
これは私の
private static final int GPS_ERROR_DIALOG_REQUEST = 9001;
MapView mMapView;
GoogleMap map;
Context CTX = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()) {
setContentView(R.layout.parents);
mMapView = (MapView) findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show();
map.addMarker(new MarkerOptions()
.position(new LatLng(4.588946, 101.125293))
.title("You are here"));
}
else
{
Toast.makeText(this, "Not ready to map!", Toast.LENGTH_SHORT).show();
}
Button AddUser = (Button) findViewById(R.id.user_reg);
Button KidsDelete = (Button) findViewById(R.id.user_del);
AddUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Parents.this, AddUser.class));
}
});
KidsDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Parents.this, KidsDelete.class));
}
});
}
public boolean servicesOK() {
int isAvailable = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(CTX);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GoogleApiAvailability.getInstance().isUserResolvableError(isAvailable)) {
Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(this, isAvailable, GPS_ERROR_DIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show();
}
return false;
}
//For mapView
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outstate) {
super.onSaveInstanceState(outstate);
mMapView.onSaveInstanceState(outstate);
}
をコード化され、これはエラー
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.moon.mykidstracker, PID: 3031
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.moon.mykidstracker/com.example.moon.mykidstracker.Parents}: 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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: 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.example.moon.mykidstracker.Parents.onCreate(Parents.java:35)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
あなた 'GoogleマップマップonMapReady方法に比べ
;' 'null'なので – Piyushは、どのようにそれを修正するのですか? @Piyush – Zaidi
Varはマップの初期化ですか? 。あなたはGoogleMapマップとして宣言しました。ありがとう。 –