簡體   English   中英

Google Maps:創建不同類別的標記

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM