lklsquare lklsquare - 2 months ago 10
Android Question

Why does my app (which uses Google Maps API 2) crash every time?

Could you please look at my code and tell me why my application crashes every time?

Main.java

package com.lakshya.maps2;

import android.app.Activity;
import android.os.Bundle;

public class Main extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

</LinearLayout>





I created the key using the SHA-1 fingerprint displaying in Eclipse > Windows > Preferences > Android > Build or something like that.

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lakshya.maps2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<permission
android:name="com.lakshya.maps2.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.lakshya.maps2.permission.MAPS_RECEIVE"/>
<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="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-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDUWhpYNe9M2w8"/>
<activity
android:name="com.lakshya.maps2.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>





Logcat errors

05-04 10:55:14.370: E/AndroidRuntime(20176): FATAL EXCEPTION: main
05-04 10:55:14.370: E/AndroidRuntime(20176): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lakshya.maps2/com.lakshya.maps2.Main}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.os.Looper.loop(Looper.java:130)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-04 10:55:14.370: E/AndroidRuntime(20176): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 10:55:14.370: E/AndroidRuntime(20176): at java.lang.reflect.Method.invoke(Method.java:507)
05-04 10:55:14.370: E/AndroidRuntime(20176): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-04 10:55:14.370: E/AndroidRuntime(20176): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-04 10:55:14.370: E/AndroidRuntime(20176): at dalvik.system.NativeStart.main(Native Method)
05-04 10:55:14.370: E/AndroidRuntime(20176): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-04 10:55:14.370: E/AndroidRuntime(20176): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.app.Activity.setContentView(Activity.java:1657)
05-04 10:55:14.370: E/AndroidRuntime(20176): at com.lakshya.maps2.Main.onCreate(Main.java:14)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-04 10:55:14.370: E/AndroidRuntime(20176): ... 11 more
05-04 10:55:14.370: E/AndroidRuntime(20176): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.lakshya.maps2-1.apk]
05-04 10:55:14.370: E/AndroidRuntime(20176): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
05-04 10:55:14.370: E/AndroidRuntime(20176): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
05-04 10:55:14.370: E/AndroidRuntime(20176): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
05-04 10:55:14.370: E/AndroidRuntime(20176): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
05-04 10:55:14.370: E/AndroidRuntime(20176): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
05-04 10:55:14.370: E/AndroidRuntime(20176): ... 20 more

Answer

You have

<uses-sdk
       android:minSdkVersion="8"

So you need to extend FragmentActivity and use SupportMapFragment.

public class Main extends Activity{

Should be

public class Main extends FragmentActivity{

This

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

should be

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

You should have the meta tag as a child of application tag

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Comments