【问题标题】:Google Maps API v2 - Android Runtime shutting down when adding a markerGoogle Maps API v2 - 添加标记时 Android 运行时关闭
【发布时间】:2015-06-04 20:17:47
【问题描述】:

A 有一个包含 MapView 的相当简单的视图。一切正常,直到我尝试从带有位置的哈希映射中添加标记。有一个非常奇怪的调试消息,VM 已关闭,屏幕只是变黑。没有例外。我正在 Google Nexus 7 2013、Android 5 上进行测试。有人遇到过这样的事情吗?

DebugActivity.java

package com.katasonov.everporter;



public class DebugActivity extends ActionBarActivity implements OnMapReadyCallback {

    static final String TAG = "DebugActivity";
    TextView mLogView;
    MapView mMapView;
    GoogleMap mMap;
    int mLogSize = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_debug);

        TextView versionName = (TextView) findViewById(R.id.versionName);

        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            versionName.setText(packageInfo.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            // should never happen
            throw new RuntimeException("Could not get package name: " + e);
        }

        mLogView = (TextView) findViewById(R.id.logView);
        mLogView.setMovementMethod(new ScrollingMovementMethod());
        mLogView.setFocusable(false);

        try {
            CSVReader logReader = new CSVReader(new FileReader(new File(getFilesDir(), "locations.csv")), ',');
            String [] nextLine;
            while ((nextLine = logReader.readNext()) != null) {
                // nextLine[] is an array of values from the line
                mLogView.append(StringUtils.join(nextLine, ", ") + "\n");
                mLogSize++;
            }
            logReader.close();
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        }

        TextView logSize = (TextView) findViewById(R.id.logSize);
        logSize.setText(String.valueOf(mLogSize));

        // map view initialization
        mMapView = (MapView) findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);
        mMapView.getMapAsync(this);
    }

    // Gets to GoogleMap from the MapView and does initialization stuff
    @Override
    public void onMapReady(GoogleMap map) {
        mMap = map;

        // a list of markers
        HashMap<String, Integer> markers = new HashMap<>();
        try {
            CSVReader logReader = new CSVReader(new FileReader(new File(getFilesDir(), "locations.csv")), ',');
            String [] nextLine;
            while ((nextLine = logReader.readNext()) != null) {
                // nextLine[] is an array of values from the line
                String key = nextLine[3] + ":" + nextLine[2];
                // check if the key exists already
                if (!markers.containsKey(key)) {
                    markers.put(key, 1);
                } else {
                    Integer val = markers.get(key);
                    val++;
                    markers.put(key, val);
                }
            }
            logReader.close();
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        }

        // iterate over markers and create them on the map
        Set<String> keys = markers.keySet();  //get all keys
        String[] lastLoc = null;
        double lat, lon;
        for(String key: keys) {
            String[] values = key.split(":");
            lastLoc = values;
            lat = Double.parseDouble(values[0]);
            lon = Double.parseDouble(values[1]);
            Log.d(TAG, "Creating marker for " + String.valueOf(lat) + ", " + String.valueOf(lon));
            map.addMarker(new MarkerOptions()
                    .title(markers.get(key) + " locations")
                    .position(new LatLng(lat, lon)));
        }

        map.setMyLocationEnabled(false);
        if (lastLoc != null) {
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(Double.parseDouble(lastLoc[0]), Double.parseDouble(lastLoc[1])), 13));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_debug, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_push_log_to_server) {
            Intent serviceIntent = new Intent(this, LocationService.class);
            serviceIntent.putExtra("uploadLogFile", true);
            startService(serviceIntent);
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_debug.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".DebugActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Version Name"
        android:id="@+id/versionName" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Log Size"
        android:id="@+id/logSize" />

    <com.google.android.gms.maps.MapView android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/logView"
        android:enabled="true"
        android:maxLines = "25"
        android:scrollbars="vertical"
        android:scrollbarStyle="insideOverlay" />

</LinearLayout>

Logcat 的一篇文章

03-31 17:41:23.157  15138-15138/com.katasonov.everporter I/x﹕ Making Creator dynamically
03-31 17:41:23.162  15138-15138/com.katasonov.everporter W/ResourcesManager﹕ Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
03-31 17:41:23.163  15138-15138/com.katasonov.everporter W/ResourcesManager﹕ Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
03-31 17:41:23.183  15138-15138/com.katasonov.everporter I/Google Maps Android API﹕ Google Play services client version: 6587000
03-31 17:41:23.195  15138-15138/com.katasonov.everporter I/Google Maps Android API﹕ Google Play services package version: 7099436
03-31 17:41:23.883  15138-15138/com.katasonov.everporter D/DebugActivity﹕ Creating marker for 49.22522522522522, 28.416659753499335
03-31 17:41:23.893  15138-15138/com.katasonov.everporter D/AndroidRuntime﹕ Shutting down VM
03-31 17:41:55.281  15138-15147/com.katasonov.everporter I/art﹕ Thread[5,tid=15147,WaitingInMainSignalCatcherLoop,Thread*=0xaf60e400,peer=0x32c07080,"Signal Catcher"]: reacting to signal 3
03-31 17:41:55.281  15138-15147/com.katasonov.everporter I/art﹕ [ 03-31 17:41:55.481   534:  556 I/Process  ]
    Sending signal. PID: 534 SIG: 3

【问题讨论】:

  • 03-31 16:03:02.162 14063-14063/com.katasonov.everporter D/AndroidRuntime﹕ Shutting down VM 行之后粘贴 logcat。您粘贴的跟踪未显示错误
  • 正如我之前提到的,没有异常也没有错误,除了这条 Debug 消息清楚地表明存在问题。发生这种情况后,我的应用程序屏幕完全变黑。如果我单击 Android 的后退按钮,则会出现一条消息“Application Everporter 没有响应”,其中包含等待或关闭选项。

标签: java android google-maps google-maps-markers google-maps-android-api-2


【解决方案1】:

好的,我将在 24 小时内回答我自己的问题! 显然,即使 Google Maps for Android docs 说您应该执行以下操作之一以确保初始化 CameraUpdateFactory:

在使用此类中的任何方法之前,您必须执行以下操作之一 以下以确保该类已初始化:

  • 等待 GoogleMap 从 MapFragment 或 MapView 变为可用 您已添加到您的应用程序。您可以验证 GoogleMap 可以通过调用 getMap() 方法并检查 返回的对象不为空。
  • 调用初始化(上下文)。作为 只要不抛出 GooglePlayServicesNotAvailableException,这 类将被正确初始化。

现实情况是,即使您已经将非空 GoogleMap 对象传递给异步调用 onMapReady(),您也需要执行第二个操作。所以当我添加了

MapsInitializer.initialize(this);

之前

map.moveCamera

它开始按预期工作 :) 享受!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-22
    • 2015-08-14
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 2013-03-14
    相关资源
    最近更新 更多