1

A有一個相當簡單的視圖,其中包含一個MapView。一切工作正常,直到我嘗試從哈希映射添加位置的標記。有一個真正的bizzare調試消息,虛擬機關閉,屏幕變黑。沒有例外。我正在Google Nexus 7 2013,Android 5上進行測試。有沒有人遇到類似的情況?Google Maps API v2 - Android Runtime在添加標記時關閉

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 
+0

在'03-31 16:03:02.162之後粘貼logcat 14063-14063/com.katasonov.everporter D/AndroidRuntime:關閉VM'。正如我前面提到的,您粘貼的跟蹤沒有顯示錯誤 – Kushal 2015-03-31 13:55:35

+0

,沒有異常並且沒有錯誤,除了此調試消息清楚地表明存在問題。發生這種情況後,我的應用程序屏幕變爲全黑如果我點擊Android的後退按鈕,則會出現一條消息「應用程序Everporter沒有響應」,並等待或關閉選項。 – intellion 2015-03-31 14:42:59

回答

3

一塊好的,我會在24小時內回答我自己的問題! 顯然,即使Google Maps for Android docs說,你應該做下列操作之一,以確保CameraUpdateFactory初始化:

此前使用從這個類的任何方法,你必須做的 之一以下,以確保此類已初始化:

  • 等待谷歌地圖從您添加到應用程序的MapFragment或MapView 。您可以通過調用getMap()方法並檢查 返回的對象是否爲空來驗證GoogleMap是否可用。
  • 調用初始化(上下文)。由於 只要不拋出GooglePlayServicesNotAvailableException,所以此類 將被正確初始化。

現實情況是,你需要做的第二,即使你已經有()傳遞到異步調用onMapReady一個非空GoogleMap對象。所以,當我加入

MapsInitializer.initialize(this); 

map.moveCamera 

它開始預期:)享受工作之前!

相關問題