【问题标题】:How do I launch and configure Google Maps API for Android Studio?如何启动和配置适用于 Android Studio 的 Google Maps API?
【发布时间】:2018-04-18 15:47:59
【问题描述】:

我复制了运行良好的代码并将其粘贴到另一台计算机上。 但是,为什么它不能在另一台计算机上工作。

  1. 工作的计算机的模拟器版本是API 26。 但是,如果您打开API 27API 25 模拟器和API 19 手机 从另一台计算机上,它将在运行的同时停止。

2.创建项目时, 我还通过谷歌主页注册了 google_maps_key google_maps_api.xml 文件。

3.我还修改了java源的包名。

我还需要设置什么?

如果你能告诉我我需要什么,我将不胜感激。

我做了 (Ctrl + G) 并附上了下面的图片。

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.

【问题讨论】:

  • 我发现标题不正确并更正了。我昨天写了一个问题,有一个sn-p没有关闭的错误,所以我尝试写另一个问题。但是之前的内容被备份了,上传了错误的内容。

标签: android google-maps android-studio google-api


【解决方案1】:
final Geocoder geocoder = new Geocoder(this);

在初始化此字段时,您尝试将Activity 用作Context 太早了。将初始化 new Geocoder(this) 移动到 onCreate() 或在活动生命周期中的更高版本。

【讨论】:

  • 我没有其他回答者,所以我接受了您的回答。很抱歉我不能提高你的分数,因为我的分数很低。
猜你喜欢
  • 2014-10-12
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多