2013-05-03 88 views
0

我一直在用这种方式撞击墙壁几天。我已经阅读过关于该主题的每一篇文章,而且似乎没有任何东西能够让我走到终点。试图让Google Play Services Map v2显示时发生ClassCastException

尝试使用Google Play Services Map v2 API构建应用程序。

我有API密钥,没问题。

这里是我的位:MainActivity.java

public class MainActivity extends FragmentActivity 
{ 

    GoogleMap googleMap; 

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

     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); 
     if (status==ConnectionResult.SUCCESS) 
     { 
      SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
      googleMap = supportMapFragment.getMap(); 
     } 
     else 
     { 
      int requestCode = 10; 
      Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); 
      dialog.show(); 
     } 
    } 

    @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; 
    } 

} 

activity_main.xml中:

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/map" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:name="com.google.android.gms.maps.MapFragment"/> 

相关清单:

<uses-sdk 
    android:minSdkVersion="11" 
    android:targetSdkVersion="17" /> 

<permission 
    android:name="com.mcdaniel.mmm4.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="com.mcdaniel.mmm4.MAPS_RECEIVE" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.persmission.INTERNET" /> 
<uses-permission android:name="android.persmission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.persmission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 

<meta-data 
    android:name="com.google.android.maps.v2.API_KEY" 
    android:value="MY API KEY" /> 

<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true" /> 

我得过去涉及的R NoClassDefFound错误$ styleable和地图类的ClassNotFound,但我无法超越这一个。

关于这个类,我尝试了几个不同的实现,但没有运气。这一个似乎是最明确的。没有编译警告。

我的目标是安卓-11分钟的android-17。我已经导入了库(带有复制文件选项),并且被我的项目引用,并且也导出了。

很明显,我错过了一些东西。请帮忙!

谢谢, 大卫

+1

你可以发布你的日志吗? – HalR 2013-05-03 04:22:13

+0

你应该从那里删除你的密钥 – JRowan 2013-05-03 05:33:55

+0

@JRowan不需要害怕揭露密钥。如果你没有他们的签名密钥和[b]公共价值,那么它是不可用的,因为APK碰到Google Play之后:没有人能阻止你安装应用程序,复制到计算机,解压缩和查看(但[a]仍然有效)。 – 2013-05-03 09:51:37

回答

0

你不能投了MapFragment(从你的XML)到SupportMapFragment

正如ankitmakwana在他的回答中所暗示的,一种可能的解决方案是将XML中的MapFragment更改为SupportMapFragment

在我看来,更好的方法是将最小支持的API设置为API12而不是API11,并使用MapFragment而不是SupportMapFragment。那么你不必担心兼容性库。

将最小支持的API设置为API12而不是API11不应该呈现(几乎)任何设备与您的应用程序不兼容,因为根据由SO最大的Android专家之一发布的this answer,基本上没有运行API11的设备。

如果你选择走这条路,只是保持你的XML,因为它是和改变从这个在你的代码下面几行:

SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
googleMap = supportMapFragment.getMap(); 

这样:

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); 
googleMap = mapFragment.getMap(); 

它将工作然后。

+0

感谢您的咨询!终于有了很多了!我改为min = 12,摆脱了所有对support.v4软件包的引用,现在我没有得到上述错误。但是我收到的是“无法加载地图,无法联系Google服务器,请在AndroidManifest.xml中添加但它已经存在! – 2013-05-03 21:01:02

+0

我认为应该有''而不是''。请尝试编辑它,并让我知道它是否工作。 – zbr 2013-05-03 21:16:08

+0

我也有这些,但错误特别引用了ACCESS_NETWORK_STATE,我也有。 – 2013-05-03 21:22:48

1

这或许可以帮助你在xml变化就像SupportMapFragment

<fragment 
      android:id="@+id/chk_aerrow_map" 
      android:name="com.google.android.gms.maps.SupportMapFragment" 
      android:layout_width="fill_parent" 
      android:layout_margin="5dp" 
      android:layout_height="100dp" /> 

详情请参阅Here

+1

简单的答案。扎布里和拉古南丹可以少打“斗殴”,并使他们的(也是最正确的)答案简明扼要。 – 2013-05-03 09:57:49

+0

你说得对。我很快会重新格式化我的答案。谢谢。 – zbr 2013-05-03 17:59:45

+0

那么帮助,但现在我回到NoClassDefFoundError:com.google.android.gms.R $ styleable ...太奇怪了。谢谢ankitmakwana! – 2013-05-03 20:24:26

0
  My target is android-17 with min of android-11 

你分钟SDK是11

注:如果您希望在api 11及更低版本上运行应用程序,您还需要添加支持库。

http://developer.android.com/tools/extras/support-library.html

使用此

android:name="com.google.android.gms.maps.SupportMapFragment" 

既然你din't添加支持库,你有ClassCastException异常

另外,还要确保你遵循了以下链接

所有步骤

https://developers.google.com/maps/documentation/android/start

编辑

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment

MapFragment的API 12和更高的支持片段的API 11和lowever

enter image description here

+0

API11 **是**蜂窝。不需要支持库。 – zbr 2013-05-03 07:59:37

+0

@Zabri我刚刚在这里回答了一个类似的问题http://stackoverflow.com/questions/16322472/google-maps-api-android-error-inflating-class-fragment/16323629#16323629。为用户工作。请在投票前检查 – Raghunandan 2013-05-03 08:03:08

+0

@Zabri检查编辑中的链接以获取更多信息。检查附带的快照中的最后一条语句。您应该在添加评论前支持您的声明。 – Raghunandan 2013-05-03 08:10:39

相关问题