2017-11-06 10 views
-1

正常に機能したコードをコピーして別のコンピュータに貼り付けました。 しかし、なぜそれは別のコンピュータで動作しませんでした。Androidスタジオ用Google Maps APIを起動して設定するにはどうすればよいですか?

  1. 作業したコンピュータのエミュレータバージョンはAPI 26でした。 しかし、別のコンピュータからAPI 27API 25エミュレータとAPI 19携帯電話 をオンにすると、同時に実行を停止します。プロジェクトを作成したら、2.

、 私も google_maps_api.xmlファイルでGoogleホームページを通じてgoogle_maps_keyを登録。

3.私はjavaソースのパッケージ名も変更しました。

何を設定する必要がありますか?

私に必要なものを教えていただければ幸いです。

私は(Ctrl + G)し、下の画像を添付しました。

enter image description here MapsActivity.java

package com.example.ggavi.myapplication; 

    import android.location.Address; 
    import android.location.Geocoder; 
    import android.support.v4.app.FragmentActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.Toast; 

    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.MarkerOptions; 

    //import java.io.IOError; 
    import java.io.IOException; 
    import java.util.List; 

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

     private EditText et; 
     private Button button; 
     private GoogleMap mMap; 
     LatLng startingPoint; 
     final Geocoder geocoder = new Geocoder(this); 

     @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; 
      et = (EditText)findViewById(R.id.editText); 
      button = (Button)findViewById(R.id.button); 

      button.setOnClickListener(new Button.OnClickListener() { 
       @Override 
       public void onClick(View view) { 

        String str = et.getText().toString(); 
        List<Address> list = null; 
        try { 
         list = geocoder.getFromLocationName(
           str, // 지역이름 
           10); // 읽을 갯수 
        }catch(IOException e) 
        { 
         e.printStackTrace(); 
        } 

        String total_str = list.get(0).toString(); 
        System.out.println(":"+total_str); 
        Toast.makeText(getApplicationContext(),total_str, Toast.LENGTH_LONG); 
        int position=total_str.indexOf("latitude"); 
        String str1 = total_str.substring(position+9); //위치 받아옴(latitude) 
        // System.out.println(">>"+str1); 
        int position_comma = str1.indexOf(","); 
        String latitude = str1.substring(0,position_comma); 
        System.out.println("latitude>>"+latitude); 
        int position2=total_str.indexOf("longitude"); 
        String str2 = total_str.substring(position2+10); //위치 받아옴(latitude) 

        int position_comma2 = str2.indexOf(","); 
        String longitude = str2.substring(0,position_comma2); 
        System.out.println("longitude>>"+longitude); 

        startingPoint = new LatLng(Double.parseDouble(latitude),Double.parseDouble(longitude)); 
        mMap.addMarker(new MarkerOptions().position(startingPoint).title("Marker in Sydney")); 
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(startingPoint,15)); 
       } 
      }); 




      // Add a marker in Sydney and move the camera 
      LatLng sydney = new LatLng(-34, 151); 
      mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
      mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 


     } 
    } 

activity_maps.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:map="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:weightSum="10"> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:orientation="vertical" 
      android:weightSum="8" 
      > 
      <EditText 
       android:id="@+id/editText" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="검색어를 입력하세요."/> 
      <Button 
       android:id="@+id/button" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="검색" 
       /> 
     </LinearLayout> 
     <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:map="http://schemas.android.com/apk/res-auto" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:id="@+id/map" 
      android:name="com.google.android.gms.maps.SupportMapFragment" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:clickable="true" 
      android:enabled="true" 
      android:layout_centerHorizontal="true" 
      android:layout_weight="2" 
      tools:context="com.example.icarus.myapplication.MapsActivity" /> 
    </LinearLayout> 

エラーコード:

11/06 21:14:21: Launching app 
$ adb shell am start -n "com.example.ggavi.myapplication/com.example.ggavi.myapplication.MapsActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER 
Client not ready yet..Connected to process 4038 on device emulator-5554 
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page. 
W/System: ClassLoader referenced unknown path: /data/app/com.example.ggavi.myapplication-1/lib/x86 
I/InstantRun: starting instant run server: is main process 
D/AndroidRuntime: Shutting down VM 
E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: com.example.ggavi.myapplication, PID: 4038 
        java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ggavi.myapplication/com.example.ggavi.myapplication.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
         at android.app.ActivityThread.-wrap12(ActivityThread.java) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:154) 
         at android.app.ActivityThread.main(ActivityThread.java:6119) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference 
         at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132) 
         at android.location.GeocoderParams.<init>(GeocoderParams.java:50) 
         at android.location.Geocoder.<init>(Geocoder.java:83) 
         at android.location.Geocoder.<init>(Geocoder.java:95) 
         at com.example.ggavi.myapplication.MapsActivity.<init>(MapsActivity.java:29) 
         at java.lang.Class.newInstance(Native Method) 
         at android.app.Instrumentation.newActivity(Instrumentation.java:1078) 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  
         at android.app.ActivityThread.-wrap12(ActivityThread.java)  
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  
         at android.os.Handler.dispatchMessage(Handler.java:102)  
         at android.os.Looper.loop(Looper.java:154)  
         at android.app.ActivityThread.main(ActivityThread.java:6119)  
         at java.lang.reflect.Method.invoke(Native Method)  
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  
Application terminated. 
+0

タイトルが正しくないとわかりました。 昨日質問を書いていましたが、スニペットが閉じなかったバグがありましたので、別の質問を書き込もうとしました。しかし、以前の内容がバックアップされ、間違った内容がアップロードされました。 – HellJosun

答えて

0
final Geocoder geocoder = new Geocoder(this); 

このフィールドを初期化するときに、ActivityContextとして早く使用しようとしています。アクティビティライフサイクルで初期化new Geocoder(this)onCreate()以降に移動します。

+0

ありがとうございました – HellJosun

+0

私はあなたの答えを受け入れたので、私は追加の回答者がいませんでした。 申し訳ありませんマークが低いのでスコアを上げることができません。 – HellJosun

関連する問題