Android google map

2020-02-01 17:58发布

I am try to add Google MAP Api v2 in my application.but I am getting exception. Exception is

 java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.googlemapsv2/info.androidhive.googlemapsv2.MainActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class fragment

Here is my xml class

<?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>

this is my main class

public class MainActivity extends Activity {
    private GoogleMap _googleMap=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try
        {
            //loading map
            initilizeMap();
            _googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

            _googleMap.setMyLocationEnabled(true);       

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void initilizeMap() {
        if(_googleMap==null)
        {
            _googleMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
            if (_googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        initilizeMap();
    }

}

this is my logcat

01-31 14:20:55.603: E/AndroidRuntime(311): FATAL EXCEPTION: main
01-31 14:20:55.603: E/AndroidRuntime(311): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demo/com.example.demo.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.os.Looper.loop(Looper.java:123)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-31 14:20:55.603: E/AndroidRuntime(311):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 14:20:55.603: E/AndroidRuntime(311):  at java.lang.reflect.Method.invoke(Method.java:521)
01-31 14:20:55.603: E/AndroidRuntime(311):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-31 14:20:55.603: E/AndroidRuntime(311):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-31 14:20:55.603: E/AndroidRuntime(311):  at dalvik.system.NativeStart.main(Native Method)
01-31 14:20:55.603: E/AndroidRuntime(311): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-31 14:20:55.603: E/AndroidRuntime(311):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.app.Activity.setContentView(Activity.java:1647)
01-31 14:20:55.603: E/AndroidRuntime(311):  at com.example.demo.MainActivity.onCreate(MainActivity.java:22)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-31 14:20:55.603: E/AndroidRuntime(311):  ... 11 more
01-31 14:20:55.603: E/AndroidRuntime(311): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.demo-2.apk]
01-31 14:20:55.603: E/AndroidRuntime(311):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
01-31 14:20:55.603: E/AndroidRuntime(311):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
01-31 14:20:55.603: E/AndroidRuntime(311):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.view.LayoutInflater.createView(LayoutInflater.java:466)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
01-31 14:20:55.603: E/AndroidRuntime(311):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
01-31 14:20:55.603: E/AndroidRuntime(311):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)

4条回答
女痞
2楼-- · 2020-02-01 18:21

If you are using Fragments your MainActivity must extends FragmentActivity:

public class MainActivity extends FragmentActivity {
    //Stuff
}

If you are using the Support-Library so you can use your Fragments in lower Android API Levels use This:

public class MainActivity extends android.support.v4.app.FragmentActivity{
    //Stuff
}

And use the SupportFragmentMap in your XML:

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
查看更多
Summer. ? 凉城
3楼-- · 2020-02-01 18:39

In your xml try to change from

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

to

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

Check if this work.

查看更多
狗以群分
4楼-- · 2020-02-01 18:44

Extend your MainActivity from FragmentActivity.

Refer this :

Inflating Google Maps v2 fragment causes ClassNotFoundException

查看更多
劳资没心,怎么记你
5楼-- · 2020-02-01 18:47

You should use SupportMapFragment instead of MapFragment.

Change

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

to

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

Also change

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

to

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

And also If you're using fragments on older devices (pre Honeycomb) you should always extend your Activity from FragmentActivity.

and also check

Add the Google Play services version to your app's manifest

Edit your application's AndroidManifest.xml file, and add the following declaration within the element. This embeds the version of Google Play services that the app was compiled with.

You just need to add <meta-data> under <application> tag into your AndroidManifest.xml

....<application>
<meta-data android:name="com.google.android.gms.version"  
 android:value="@integer/google_play_services_version" />
</application>

This is because latest google play services requires a version name, which is to be mentioned using <meta-data .. /> inside AndroidManifest.xml

查看更多
登录 后发表回答