我正在努力将谷歌地图合并到android

时间:2014-12-03 19:17:12

标签: android google-maps

我还在学习android编程的细节。 我的应用程序每次都会立即关闭 演练结束后我完成了演练

有人能帮助我吗?

清单:          

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>    
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="keyvaluehere"/>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

MainActivity.java     package com.example.hellomap;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, 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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

.xml文件

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/maps"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.MapFragment"/>

所以是的

1 个答案:

答案 0 :(得分:0)

我遇到类似地图片段崩溃我的应用程序的问题。解决崩溃的方法是用Java而不是xml初始化地图片段。更具体地说,我在地图的xml中使用了FrameLayout容器,并使用FragmentManager处理了Java中的所有碎片。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <FrameLayout
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

...

import android.gms.maps;
import android.gms.maps.model;

...

MapFragment mapFrag = (MapFragment)FragmentManager.FindFragmentByTag("map");
if(mapFrag == null){
   FragmentTransaction fragTran = FragmentManager.BeginTransaction();
   mapFrag = MapFragment.NewInstance();
   fragTran.Add(Resource.Id.map, mapFrag, "map");
   fragTran.Commit();
}
GoogleMap googleMap = mapFrag.Map;