Мое приложение для Android не работает, когда я перестраиваю и разворачиваю - PullRequest
0 голосов
/ 15 июня 2019

У меня есть код, показывающий карту Google, и пользователь может просмотреть адрес местоположения, нажав на карту. Когда я впервые запускаю программное обеспечение на своем телефоне, оно работает. Но когда я изменяю внешний вид программного обеспечения, например изменяю изображение значка программного приложения, это не похоже на первый. Показывает карту, но при нажатии на карту не показывает адрес. Кроме того, если я создаю новый проект с тем же кодом, он работает нормально в первый раз. Но из следующих нескольких раз, он показывает только карту.

package com.example.ghappmgis4;

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
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.IOException;
import java.util.List;
import java.util.Locale;

public class GoogleSatelliteMapShow extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.google_satellite_map);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.googlesatellitemap);
        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;

        // Add a marker in Sydney and move the camera
        LatLng Tehran = new LatLng(35, 51);
        mMap.addMarker(new MarkerOptions().position(Tehran).title("ایران"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(Tehran));
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        mMap.setOnMapClickListener((new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng point) {
                //Toast.makeText(getBaseContext(), " click", Toast.LENGTH_SHORT).show();
                Log.d("DEBUG","Map clicked[" + point.latitude + "/" + point.longitude + "]");
                Geocoder geoCoder= new Geocoder(getBaseContext(), Locale.getDefault());
                try {
                    List<Address> addresses = geoCoder.getFromLocation(point.latitude, point.longitude, 1);
                    String add = "";
                    if (addresses.size() > 0) {
                        /**for (int i = 0; i < addresses.get(0).getMaxAddressLineIndex(); i++)
                         add += addresses.get(0).getAddressLine(i)+" ";**/
                        add += addresses.get(0).getCountryName()+" ";
                        add += addresses.get(0).getAddressLine(0)+" ";

                    }
                    Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
                }
                catch (IOException e){
                    e.printStackTrace();
                }
            }

        }));

    }
    public boolean onKeyDown(int keyCode, KeyEvent event){
        switch (keyCode) {
            case KeyEvent.KEYCODE_3:
                mMap.animateCamera(CameraUpdateFactory.zoomIn());
                break;
            case KeyEvent.KEYCODE_1:
                mMap.animateCamera(CameraUpdateFactory.zoomOut());
                break;
        }
        return super.onKeyDown(keyCode,event);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<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/googlesatellitemap"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    map:uiZoomControls="true"
    tools:context=".GoogleSatelliteMapShow"

/>

06-16 00:13:27.573 30458-30458/com.example.ghappmgis4 D/DEBUG: Map clicked[2.8225890364293718/51.04967750608921]
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err: java.io.IOException: grpc failed
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at android.location.Geocoder.getFromLocation(Geocoder.java:136)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at com.example.ghappmgis4.GoogleSatelliteMapShow$1.onMapClick(GoogleSatelliteMapShow.java:63)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at com.google.android.gms.maps.zzy.onMapClick(Unknown Source)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at com.google.android.gms.maps.internal.zzak.dispatchTransaction(Unknown Source)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at android.os.Binder.transact(Binder.java:380)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at hn.b(:com.google.android.gms.dynamite_mapsdynamite@17528027@17.5.28 (020300-252519129):14)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at com.google.android.gms.maps.internal.av.a(:com.google.android.gms.dynamite_mapsdynamite@17528027@17.5.28 (020300-252519129):4)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at com.google.maps.api.android.lib6.gmm6.api.aa.b(:com.google.android.gms.dynamite_mapsdynamite@17528027@17.5.28 (020300-252519129):8)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at com.google.maps.api.android.lib6.gmm6.vector.ah.b(:com.google.android.gms.dynamite_mapsdynamite@17528027@17.5.28 (020300-252519129):132)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at com.google.maps.api.android.lib6.gmm6.vector.da.onSingleTapConfirmed(:com.google.android.gms.dynamite_mapsdynamite@17528027@17.5.28 (020300-252519129):22)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at com.google.maps.api.android.lib6.impl.gesture.a.onSingleTapConfirmed(:com.google.android.gms.dynamite_mapsdynamite@17528027@17.5.28 (020300-252519129):240)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at com.google.maps.api.android.lib6.impl.gesture.c.handleMessage(:com.google.android.gms.dynamite_mapsdynamite@17528027@17.5.28 (020300-252519129):9)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at android.os.Looper.loop(Looper.java:135)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5257)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
06-16 00:13:27.943 30458-30458/com.example.ghappmgis4 W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
06-16 00:13:27.953 30458-30458/com.example.ghappmgis4 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
06-16 00:13:33.863 30458-30475/com.example.ghappmgis4 I/art: Explicit concurrent mark sweep GC freed 3664(144KB) AllocSpace objects, 0(0B) LOS objects, 56% free, 9MB/21MB, paused 1.489ms total 58.644ms
...