1

我正在尝试使用 Android 加载简单地图,但是当我在移动设备上运行此应用程序时出现问题。这是我的日志猫

07-22 14:47:12.207: D/AndroidRuntime(3241): Shutting down VM
07-22 14:47:12.207: W/dalvikvm(3241): threadid=1: thread exiting with uncaught exception (group=0x40d17258)
07-22 14:47:12.215: E/AndroidRuntime(3241): FATAL EXCEPTION: main
07-22 14:47:12.215: E/AndroidRuntime(3241): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adeel.map/com.adeel.map.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2001)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2028)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1179)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.os.Looper.loop(Looper.java:137)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.app.ActivityThread.main(ActivityThread.java:4508)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at java.lang.reflect.Method.invokeNative(Native Method)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at java.lang.reflect.Method.invoke(Method.java:511)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at dalvik.system.NativeStart.main(Native Method)
07-22 14:47:12.215: E/AndroidRuntime(3241): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:699)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:741)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.view.LayoutInflater.inflate(LayoutInflater.java:491)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:252)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.app.Activity.setContentView(Activity.java:1837)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at com.adeel.map.MainActivity.onCreate(MainActivity.java:23)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.app.Activity.performCreate(Activity.java:4479)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1965)
07-22 14:47:12.215: E/AndroidRuntime(3241):     ... 11 more
07-22 14:47:12.215: E/AndroidRuntime(3241): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
07-22 14:47:12.215: E/AndroidRuntime(3241):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:671)
07-22 14:47:12.215: E/AndroidRuntime(3241):     ... 21 more
07-22 14:47:14.132: I/Process(3241): Sending signal. PID: 3241 SIG: 9

这是我的源代码

package com.adeel.map;

import android.os.Bundle;
import android.view.Menu;
import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment;

import com.google.android.gms.maps.GoogleMap;
import java.io.InputStream;
import org.json.JSONArray;
import com.adeel.map.R;



public class MainActivity extends FragmentActivity  {
    JSONArray jArray;
    String result = null;
    InputStream is = null;
    StringBuilder sb=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //GoogleMap mMap;
        //mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        //String readTwitterFeed = readpoints();
        SupportMapFragment fm = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map);
        GoogleMap mMap = fm.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;
    }

}

这是我的 XML

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

这是清单

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

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

    <permission
        android:name="com.adeel.map.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

    <uses-permission android:name="com.adeel.map.maps.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:debuggable="true" >
        <activity
            android:name="com.adeel.map.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>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCHTWS_mHN-2a0oTC2NiC4_a0......." />
    </application>

</manifest>

任何人请帮助我这里有什么问题。

4

1 回答 1

0

问题是您在 XML 中使用:

class="com.google.android.gms.maps.MapFragment"

反而:

class="com.google.android.gms.maps.SupportMapFragment"

这是错误:

Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
于 2013-07-22T10:01:28.367 回答