我为地图制作了一款应用。添加了所有必要的权限并生成了api密钥,但它仍然崩溃。我正在使用命令行并在Android 4.3操作系统上实现它。
以下是代码:
public class MainActivity extends FragmentActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
清单文件
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<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="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<permission
android:name="mapdemo.exp.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="mapdemo.exp.permission.MAPS_RECEIVE"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<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>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAS08XqNbdRuxsOwNrosza0WE2mijfdoO8"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="4323000" />
</application>
试过这个
public class ActivityMain extends Activity
{
public GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
Log.e("loading map. . . .", "loading map. . . ");
initilizeMap();
}
catch (Exception e)
{
Log.e("catch. . .", "catch. . .");
e.printStackTrace();
}
}
private void initilizeMap()
{
Log.e("initializing map. . . ", "initializing map. . ");
if (googleMap == null)
{
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (googleMap == null)
{
Toast.makeText(getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onResume()
{
super.onResume();
initilizeMap();
}
}
答案 0 :(得分:0)
试试这个
在xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"
/>
</LinearLayout>
在活动中: -
public class ActivityMain extends Activity {
// Google Map
public GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Log.e("loading map. . . .", "loading map. . . ");
// Loading map
initilizeMap();
} catch (Exception e) {
Log.e("catch. . .", "catch. . .");
e.printStackTrace();
}
/*
* MapFunctionality mf = new MapFunctionality(); mf.currentLoc();
* mf.addMarker();
*/
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
// MapFunctionality mf = new MapFunctionality();
Log.e("initializing map. . . ", "initializing map. . ");
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
// mf.currentLoc();
// mf.addMarker();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}