2016-06-29 4 views
0

私のアンドロイドスタジオプロジェクトでは、2つのアクティビティがあります:1つはMapsActivity、もう1つは基本的です。Googleマップ:別のクラスからマーカーを作成

マップをクリックすると、マーカーのカスタムプロパティを定義できるEditTexts(テキストフィールド)のセットで構成される他のアクティビティが起動します。しかし、そのクラスからマーカーを作成しようとすると、Android Studioによってエラーが表示されます。

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) 
ここ

コード、MapsActivity.javaであります

どうすればこの問題を解決できますか?任意の入力ウィル感謝される:)

答えて

2

ああ男の子、あなたはいくつかのことを学ぶ必要があります。

最初にActivityのコンストラクタを呼び出すべきではありません。フレームワークがActivityのインスタンスを作成し、onCreate(Bundle savedInstanceState)に初期化ロジックを処理するという考え方です。コンストラクタを呼び出すだけであれば、UIの一部ではないランダムオブジェクトを作成します。それには、MapsActivityの場合はインスタンスを設定します。あなたが別のActivityからMapsActivityを更新したい場合は、2番目の

newMarkerは、あなたがJavaの規則を維持し、大文字でNewMarkerActivityのようなものをクラス名を開始する必要があり、

サード恐ろしいクラス名です最初のアクティビティの結果の場合は、2番目のアクティビティを開始する必要があります。基本的には、現在の通話ではなくstartActivityForResult(new Intent(MapsActivity.this, NewMarkerActivity.class))に電話をかけ、onActivityResultMapsActivityに上書きする必要があります。 結果をBundleに追加してタイトルを渡し、onActivityResultで取得します。

Consult the documentation for getting results

編集: タイトルを渡すには、Intentを作成し、Intent.putExtra("someKey", theTitle)を呼び出して、タイトルに渡す必要があります。その後、 とActivity.setResult(int, Intent)Intentと呼んでください。 onActivityResultでは、あなたはそれはあなたがタイプミスをしないように、あなたのNewMarkerActivityクラスのpublic static finalフィールドそのキーを作成することをお勧めしますgetStringExtra("someKey")

を呼び出すことにより、タイトルを得ることができます。

+0

ご連絡ありがとうございました。結果を受け取ったら、私は意図を持っていますが、私は余分なものとして 'タイトル'をどのように渡すのか分かりません。ドキュメンテーションは役に立ちません。 –

+0

問題ありません。あなたのタイトルをどのように渡すことができるかを見るために編集をチェックしてください。 – npace

+0

ああ、そう、右、素晴らしい音!もう一度助けてくれてありがとう。 –

0

このようにマップのインスタンスを取得することはできません。 mMapフィールドがnullに設定されているMapsActivityオブジェクトの新しいインスタンスを作成しています。 EditTextからnewMarkersetResult(int, Intent)で値を返す必要があります。ここをクリックしてください:Getting a Result from an Activity

+1

@npaceは速く、彼は批判するのを恐れていなかったので、私は彼の返答に言及すべきです。 – egoldx

関連する問題