ここに典型的な問題があります。Android マップを正常にロードしようとしましたが、スプラッシュ スクリーンも正常にロードしようとしました。
スプラッシュ画面の後にマップを読み込もうとすると、次の例外が発生します
04-08 00:35:46.890: E/AndroidRuntime(19891): FATAL EXCEPTION: main
04-08 00:35:46.890: E/AndroidRuntime(19891): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
04-08 00:35:46.890: E/AndroidRuntime(19891): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
04-08 00:35:46.890: E/AndroidRuntime(19891): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.app.Activity.onCreateView(Activity.java:4242)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-08 00:35:46.890: E/AndroidRuntime(19891): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.app.Activity.setContentView(Activity.java:1835)
04-08 00:35:46.890: E/AndroidRuntime(19891): at com.example.androidmapsv2.MainActivity.onCreate(MainActivity.java:53)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.app.Activity.performCreate(Activity.java:4465)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.os.Looper.loop(Looper.java:137)
04-08 00:35:46.890: E/AndroidRuntime(19891): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-08 00:35:46.890: E/AndroidRuntime(19891): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 00:35:46.890: E/AndroidRuntime(19891): at java.lang.reflect.Method.invoke(Method.java:511)
04-08 00:35:46.890: E/AndroidRuntime(19891): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-08 00:35:46.890: E/AndroidRuntime(19891): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-08 00:35:46.890: E/AndroidRuntime(19891): at dalvik.system.NativeStart.main(Native Method)
04-08 00:35:49.703: I/Process(19891): Sending signal. PID: 19891 SIG: 9
マップの埋め込みとスプラッシュ画面の表示の基本的な手順はすべて、個別に実装してみたら成功したので、すべて実行しました。
google-play-services jar と android-supportv4 jar が libs フォルダーにあり、適切に参照されていることを確認しました。
MainActivity.java
package com.example.androidmapsv2;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
//import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
//import android.support.v4.app.FragmentActivity;
//import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnMapClickListener,
OnMapLongClickListener, LocationListener {
final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;
Location myLocation;
TextView tvLocInfo;
LocationListener myLocationListener;
double CURRENT_LAT = 0.0;
double CURRENT_LON = 0.0;
double PREVIOUS_LAT = 0.0;
double PREVIOUS_LON = 0.0;
LocationManager locationManager;
String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvLocInfo = (TextView) findViewById(R.id.locinfo);
FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment = (MapFragment) myFragmentManager
.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();
myMap.setMyLocationEnabled(true);
// myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// myMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
// Getting LocationManager object
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Creating an empty criteria object
Criteria criteria = new Criteria();
// Getting the name of the provider that meets the criteria
provider = locationManager.getBestProvider(criteria, false);
if (provider != null && !provider.equals("")) {
// Get the location from the given provider
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 1, this);
if (location != null) {
onLocationChanged(location);
} else {
Toast.makeText(getBaseContext(), "Location can't be retrieved",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getBaseContext(), "No Provider Found",
Toast.LENGTH_SHORT).show();
}
myMap.setOnMapClickListener(this);
myMap.setOnMapLongClickListener(this);
}
@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
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_legalnotices:
String LicenseInfo = GooglePlayServicesUtil
.getOpenSourceSoftwareLicenseInfo(getApplicationContext());
AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(
MainActivity.this);
LicenseDialog.setTitle("Legal Notices");
LicenseDialog.setMessage(LicenseInfo);
LicenseDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS) {
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS", Toast.LENGTH_LONG)
.show();
} else {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
RQS_GooglePlayServices);
}
}
@Override
public void onMapClick(LatLng point) {
tvLocInfo.setText(point.toString());
myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
}
@Override
public void onMapLongClick(LatLng point) {
tvLocInfo.setText("New marker added@" + point.toString());
myMap.addMarker(new MarkerOptions().position(point).title(
point.toString()));
// Polyline line =
// myMap.addPolyline(new PolylineOptions().add(
// new LatLng(12.97202903709172, 80.21577659994364),
// new LatLng(12.978670181826473, 80.2174412459135))
// .geodesic(true));
double lat = point.latitude;
double lon = point.longitude;
System.out.println("lat = "+lat);
CURRENT_LAT = lat;
CURRENT_LON = lon;
tvLocInfo.setText("lat: " + lat + "\n" + "lon: " + lon);
LatLng latlng = new LatLng(lat,
lon);
myMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));
if (CURRENT_LAT != 0.0 && CURRENT_LON != 0.0 && PREVIOUS_LAT != 0.0
&& PREVIOUS_LON != 0.0) {
myMap.addPolyline(new PolylineOptions().add(
new LatLng(PREVIOUS_LAT, PREVIOUS_LON),
new LatLng(CURRENT_LAT, CURRENT_LON)).geodesic(true));
}
PREVIOUS_LAT = CURRENT_LAT;
PREVIOUS_LON = CURRENT_LON;
}
@Override
public void onLocationChanged(Location myLocation) {
// TODO Auto-generated method stub
// if (myLocationListener != null) {
// myLocationListener.onLocationChanged(myLocation);
double lat = myLocation.getLatitude();
double lon = myLocation.getLongitude();
CURRENT_LAT = lat;
CURRENT_LON = lon;
tvLocInfo.setText("lat: " + lat + "\n" + "lon: " + lon);
LatLng latlng = new LatLng(myLocation.getLatitude(),
myLocation.getLongitude());
myMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));
if (CURRENT_LAT != 0.0 && CURRENT_LON != 0.0 && PREVIOUS_LAT != 0.0
&& PREVIOUS_LON != 0.0) {
myMap.addPolyline(new PolylineOptions().add(
new LatLng(PREVIOUS_LAT, PREVIOUS_LON),
new LatLng(CURRENT_LAT, CURRENT_LON)).geodesic(true));
}
PREVIOUS_LAT = CURRENT_LAT;
PREVIOUS_LON = CURRENT_LON;
// }
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
SplashScreen.java
package com.example.androidmapsv2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
private static final int SPLASH_TIME = 3 * 1000;// 3 seconds
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashScreen.this,
MainActivity.class);
startActivity(intent);
SplashScreen.this.finish();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}, SPLASH_TIME);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
}
}, SPLASH_TIME);
}
@Override
public void onBackPressed() {
this.finish();
super.onBackPressed();
}
}
activity_main.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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/locinfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
</LinearLayout>
スプラッシュスクリーン.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:background="@drawable/splash"
android:layout_height="fill_parent"
android:orientation="vertical" >
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidmapsv2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<permission
android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" >
</permission>
<uses-permission android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<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="AIzaSyAkfu2ZAgQfO6iQ_ldkOAaVyMOPDmq_Lg0" />
<activity
android:name="com.example.androidmapsv2.SplashScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.androidmapsv2.MainActivity" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="My Key" />
</application>
</manifest>
以下の構造を参照してください
また、最新の API キーとその他の基本的なものを使用しました。