尝试将Google Play Services Map v2显示时出现ClassCastException

时间:2013-05-03 04:18:17

标签: android google-maps-android-api-2

我几天来一直在用墙打我的头。我已经阅读了关于这个话题的每一篇文章,似乎没有什么能让我跨越终点。

尝试使用Google Play服务地图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 $ styleable的NoClassDefFound错误以及maps类的ClassNotFound,但是我无法通过这个错误。

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

我的目标是android-17,其中min-android-11。我已导入库(带有复制文件选项),并由我的项目引用,也导出。

显然有些东西我不见了。请帮忙!

谢谢, 大卫

3 个答案:

答案 0 :(得分: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 :(得分:0)

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

你的min sdk是11。

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

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

使用此

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

由于您没有添加支持库,因此您获得了classcastexception

另请确保您已按照以下链接中的所有步骤

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

修改

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

api 12及更高支持片段的MapFragment for api 11和lowever

enter image description here

答案 2 :(得分:0)

您无法将MapFragment(来自您的XML)投射到SupportMapFragment

一种可能的解决方案是将XML中的MapFragment更改为SupportMapFragment,如ankitmakwana在答案中所述。

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

将最小支持的API设置为API12而不是API11不应该渲染(几乎)与您的应用程序不兼容的任何设备,正如SO {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();

它会起作用。