我是android的初学者......我正在创建一个应用程序来加载谷歌地图v2。但它没有加载并且每次都会崩溃并显示一条消息不幸已关闭。请帮助我解决错误..提前致谢..我是一个普通的追随者堆栈溢出..
**my code**
**Main activity.java**
package com.mypack.test;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Toast;
public class MainActivity extends Activity {
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
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();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}
测试清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypack.test"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.mypack.test.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.mypack.test.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<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" >
<activity
android:name="com.mypack.test.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>
<!-- Goolge API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDnHxpS7Pb6YeKBmyWjV_Slj9mIjo6M1FM" />
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
日志猫
09-19 09:23:08.785: E/Trace(803): error opening trace file: No such file or directory (2)
09-19 09:23:09.515: W/dalvikvm(803): VFY: unable to resolve static field 1065 (MapAttrs) in Lcom/google/android/gms/R$styleable;
09-19 09:23:09.515: D/dalvikvm(803): VFY: replacing opcode 0x62 at 0x000e
09-19 09:23:09.525: D/AndroidRuntime(803): Shutting down VM
09-19 09:23:09.525: W/dalvikvm(803): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-19 09:23:09.535: E/AndroidRuntime(803): FATAL EXCEPTION: main
09-19 09:23:09.535: E/AndroidRuntime(803): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
09-19 09:23:09.535: E/AndroidRuntime(803): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
09-19 09:23:09.535: E/AndroidRuntime(803): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.app.Activity.onCreateView(Activity.java:4716)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-19 09:23:09.535: E/AndroidRuntime(803): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.app.Activity.setContentView(Activity.java:1881)
09-19 09:23:09.535: E/AndroidRuntime(803): at com.mypack.test.MainActivity.onCreate(MainActivity.java:17)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.app.Activity.performCreate(Activity.java:5104)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.os.Looper.loop(Looper.java:137)
09-19 09:23:09.535: E/AndroidRuntime(803): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-19 09:23:09.535: E/AndroidRuntime(803): at java.lang.reflect.Method.invokeNative(Native Method)
09-19 09:23:09.535: E/AndroidRuntime(803): at java.lang.reflect.Method.invoke(Method.java:511)
09-19 09:23:09.535: E/AndroidRuntime(803): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-19 09:23:09.535: E/AndroidRuntime(803): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-19 09:23:09.535: E/AndroidRuntime(803): at dalvik.system.NativeStart.main(Native Method)
编辑之前的代码后新的 logcat.. 并加载 playservice 和 v4.jar
09-19 14:04:10.644: D/dalvikvm(1930): GC_CONCURRENT freed 1650K, 22% free 6514K/8284K, paused 76ms+19ms, total 279ms
09-19 14:04:10.644: D/dalvikvm(1930): WAIT_FOR_CONCURRENT_GC blocked 51ms
09-19 14:04:11.121: I/Choreographer(1930): Skipped 51 frames! The application may be doing too much work on its main thread.
09-19 14:04:13.281: I/Choreographer(1930): Skipped 170 frames! The application may be doing too much work on its main thread.
09-19 14:05:14.910: D/dalvikvm(1930): GC_CONCURRENT freed 85K, 7% free 7779K/8284K, paused 71ms+5ms, total 151ms
09-19 14:05:14.910: D/dalvikvm(1930): WAIT_FOR_CONCURRENT_GC blocked 64ms
09-19 14:05:15.040: E/MediaPlayer(1930): prepareAsync called in state 8
09-19 14:05:15.040: W/System.err(1930): java.lang.IllegalStateException
09-19 14:05:15.040: W/System.err(1930): at android.media.MediaPlayer.prepare(Native Method)
09-19 14:05:15.050: W/System.err(1930): at androassist.mypack.splash.onCreate(splash.java:38)
09-19 14:05:15.050: W/System.err(1930): at android.app.Activity.performCreate(Activity.java:5104)
09-19 14:05:15.060: W/System.err(1930): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-19 14:05:15.060: W/System.err(1930): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-19 14:05:15.060: W/System.err(1930): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-19 14:05:15.060: W/System.err(1930): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-19 14:05:15.060: W/System.err(1930): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-19 14:05:15.060: W/System.err(1930): at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 14:05:15.060: W/System.err(1930): at android.os.Looper.loop(Looper.java:137)
09-19 14:05:15.060: W/System.err(1930): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-19 14:05:15.071: W/System.err(1930): at java.lang.reflect.Method.invokeNative(Native Method)
09-19 14:05:15.071: W/System.err(1930): at java.lang.reflect.Method.invoke(Method.java:511)
09-19 14:05:15.071: W/System.err(1930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-19 14:05:15.080: W/System.err(1930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-19 14:05:15.080: W/System.err(1930): at dalvik.system.NativeStart.main(Native Method)
09-19 14:05:20.561: I/Choreographer(1930): Skipped 39 frames! The application may be doing too much work on its main thread.
09-19 14:05:21.551: I/Choreographer(1930): Skipped 222 frames! The application may be doing too much work on its main thread.
09-19 14:05:22.491: I/Choreographer(1930): Skipped 225 frames! The application may be doing too much work on its main thread.
09-19 14:05:26.330: W/IInputConnectionWrapper(1930): showStatusIcon on inactive InputConnection
09-19 14:05:28.770: D/Notice(1930): Entering log
09-19 14:05:28.920: D/Key(1930): key matched
09-19 14:05:32.620: I/Choreographer(1930): Skipped 783 frames! The application may be doing too much work on its main thread.
09-19 14:05:33.422: I/Choreographer(1930): Skipped 174 frames! The application may be doing too much work on its main thread.
09-19 14:05:35.290: I/Choreographer(1930): Skipped 55 frames! The application may be doing too much work on its main thread.
09-19 14:05:59.101: D/Notice(1930): Entering log
09-19 14:05:59.831: I/Choreographer(1930): Skipped 48 frames! The application may be doing too much work on its main thread.
09-19 14:06:00.600: I/MyApp(1930): Silent mode
09-19 14:06:00.710: I/Choreographer(1930): Skipped 115 frames! The application may be doing too much work on its main thread.
09-19 14:06:01.280: I/Choreographer(1930): Skipped 48 frames! The application may be doing too much work on its main thread.
09-19 14:06:15.223: D/Notice(1930): Entering log
09-19 14:06:15.900: I/Choreographer(1930): Skipped 54 frames! The application may be doing too much work on its main thread.
09-19 14:06:16.712: I/MyApp(1930): Silent mode
09-19 14:06:16.930: I/Choreographer(1930): Skipped 187 frames! The application may be doing too much work on its main thread.
09-19 14:06:17.620: I/Choreographer(1930): Skipped 92 frames! The application may be doing too much work on its main thread.
09-19 14:06:26.514: D/Notice(1930): Entering log
09-19 14:06:27.103: I/Choreographer(1930): Skipped 50 frames! The application may be doing too much work on its main thread.
09-19 14:06:28.241: D/GPS(1930): GPS Enabled
09-19 14:06:28.265: D/loc message:(1930): fmygpslo(0.00.0)
09-19 14:06:28.731: I/Choreographer(1930): Skipped 381 frames! The application may be doing too much work on its main thread.
09-19 14:06:34.700: I/Choreographer(1930): Skipped 177 frames! The application may be doing too much work on its main thread.
感谢您花费宝贵的时间。我得到了解决方案......这是我的错误......正如 Reghunandan 和 Patrik 所说。引用 google playstore 服务有问题..我的应用程序从 SDK 目录引用 google playstore 服务,而不是从在我的工作区中加载了谷歌 Playstore 的版本。所以上面的错误被清除了..在运行应用程序时,地图没有加载..我用下面的代码检查了它
package myapp.mypack;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Toast;
public class MapActivity extends FragmentActivity {
String msgtype,sendernum;
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(check())
{
setContentView(R.layout.app_main);
Toast.makeText(this, "In Google play service.", Toast.LENGTH_LONG).show();
initFun();
}
else
{
Toast.makeText(this, "Google play service not available.", Toast.LENGTH_LONG).show();
}
}
private Boolean check()
{
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(status==ConnectionResult.SUCCESS)
{
return true;
}
return false;
}
public void initFun()
{
SupportMapFragment sp = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = sp.getMap();
googleMap.setMyLocationEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map, menu);
return true;
}
}
安卓清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myapp.mypack"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="myapp.mypack.googlemapsv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="myapp.mypack.googlemapsv2.permission.MAPS_RECEIVE"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<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" >
<activity
android:name="myapp.mypack.MapActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Goolge API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDnHxpS7Pb6YeKBmyWjV_Slj9mIjo6M1FM" />
</application>
</manifest>
但是我的地图正在加载...相反,它会显示一段时间的屏幕并显示“Google play 服务不可用”。如代码中所指定..但我检查了我的项目>属性> android。google playstore 库有一个绿色的勾号。所以认为这是我的Playstore版本的问题..*我在我的galaxy s3 mini中运行该应用程序。但它也显示相同的白屏..我在手机中的Playstore构建版本是:4.3.11。我唯一的解决方案是更新playstore并重新运行我的应用程序。logcat还显示我手机的playstore版本需要更新..请大家看看我的查询..是我手机的playstore版本有问题吗.. *
日志猫
09-20 01:19:51.980: E/Trace(26515): error opening trace file: No such file or directory (2)
09-20 01:19:52.020: W/GooglePlayServicesUtil(26515): Google Play services out of date. Requires 3264100 but found 3225134
09-20 01:19:52.190: D/libEGL(26515): loaded /system/lib/egl/libEGL_mali.so
09-20 01:19:52.210: D/libEGL(26515): loaded /system/lib/egl/libGLESv1_CM_mali.so
09-20 01:19:52.210: D/libEGL(26515): loaded /system/lib/egl/libGLESv2_mali.so
09-20 01:19:52.240: D/OpenGLRenderer(26515): Enabling debug mode 0
**Please help me to find a solution?**