“Google地图v2上的类错误”

时间:2014-10-26 21:14:20

标签: java android google-maps android-fragments

我还在搜索,我知道这个问题存在很多问题,但没有人解决我的问题。这是我的代码:

MainActivity.java

package com.pipolproj.pipol;

import com.google.android.gms.maps.GoogleMap;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.maps.MapFragment; 

public class MainActivity extends Activity {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.pipolproj.pipol.MainActivity" >

    <fragment class="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        map:cameraBearing="112.5"
        map:cameraTargetLat="-33.796923"
        map:cameraTargetLng="150.922433"
        map:cameraTilt="30"
        map:cameraZoom="13"
        map:mapType="normal"
        map:uiCompass="false"
        map:uiRotateGestures="true"
        map:uiScrollGestures="false"
        map:uiTiltGestures="true"
        map:uiZoomControls="false"
        map:uiZoomGestures="true"/>

</RelativeLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pipolproj.pipol"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <!-- Permission -->
    <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="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

    <!--  OpenGL -->
    <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=".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>

        <!--  Google   -->
        <meta-data 
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="REMOVED"
        />
    </application>
</manifest>

错误:

10-26 21:11:54.006: E/AndroidRuntime(2580): FATAL EXCEPTION: main
10-26 21:11:54.006: E/AndroidRuntime(2580): Process: com.pipolproj.pipol, PID: 2580
10-26 21:11:54.006: E/AndroidRuntime(2580): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pipolproj.pipol/com.pipolproj.pipol.MainActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.os.Looper.loop(Looper.java:135)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.app.ActivityThread.main(ActivityThread.java:5221)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at java.lang.reflect.Method.invoke(Native Method)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at java.lang.reflect.Method.invoke(Method.java:372)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-26 21:11:54.006: E/AndroidRuntime(2580): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.app.Activity.setContentView(Activity.java:2144)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at com.pipolproj.pipol.MainActivity.onCreate(MainActivity.java:18)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.app.Activity.performCreate(Activity.java:5933)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
10-26 21:11:54.006: E/AndroidRuntime(2580):     ... 10 more
10-26 21:11:54.006: E/AndroidRuntime(2580): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.google.android.gms.maps.SupportMapFragment that is not a Fragment
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.app.Fragment.instantiate(Fragment.java:606)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.app.Fragment.instantiate(Fragment.java:582)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.app.Activity.onCreateView(Activity.java:5282)
10-26 21:11:54.006: E/AndroidRuntime(2580):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
10-26 21:11:54.006: E/AndroidRuntime(2580):     ... 20 more
10-26 21:11:54.006: E/AndroidRuntime(2580): Caused by: java.lang.ClassCastException
10-26 21:11:54.006: E/AndroidRuntime(2580):     ... 25 more

我做完了:

  • 项目 - &gt;属性 - &gt; Android - &gt;在库上添加google-play-services_lib
  • 项目 - &gt;属性 - &gt; Java构建路径 - &gt;图书馆 - &gt;添加android-support-v4.jar和android-support-v13.jar

我不知道该怎么做:\

1 个答案:

答案 0 :(得分:0)

在您的布局中,您在android:name中有SupportMapFragment,但在Activity类中,您正尝试将其分配给MapFragment对象。

如果你的目标是android版本4+,那么在两个地方使用MapFragment,否则使用SupportMapFragment