rCan rCan - 4 months ago 6
Java Question

Simple map app on Android(google api v2). Doesnt work. Error

I try to program an easy map app on Android but the only outcome are errors. I tried to program it the way it should be but it still doesn't work.

mainactivity

package com.example.guidebook;

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

public class GuidebookActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guidebook);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.guidebook, 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" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>

</RelativeLayout>


manifest

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

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<permission
android:name="com.example.guidebook.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>

<uses-permission android:name="com.example.guidebook.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<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"/>
<!-- Maps API needs OpenGL ES 2.0. -->
<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" >
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AI-------------------"/>
<activity
android:name="com.example.guidebook.GuidebookActivity"
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>


I am new on this area and therefore ask for detailed help.

Log: 12-09 10:35:44.200: D/AndroidRuntime(24663): Shutting down VM
12-09 10:35:44.200: W/dalvikvm(24663): threadid=1: thread exiting with uncaught exception (group=0x415d5ba8)
12-09 10:35:44.200: E/AndroidRuntime(24663): FATAL EXCEPTION: main
12-09 10:35:44.200: E/AndroidRuntime(24663): Process: com.example.guidebook, PID: 24663
12-09 10:35:44.200: E/AndroidRuntime(24663): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.guidebook/com.example.guidebook.GuidebookActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.os.Handler.dispatchMessage(Handler.java:102)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.os.Looper.loop(Looper.java:136)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-09 10:35:44.200: E/AndroidRuntime(24663): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 10:35:44.200: E/AndroidRuntime(24663): at java.lang.reflect.Method.invoke(Method.java:515)
12-09 10:35:44.200: E/AndroidRuntime(24663): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-09 10:35:44.200: E/AndroidRuntime(24663): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-09 10:35:44.200: E/AndroidRuntime(24663): at dalvik.system.NativeStart.main(Native Method)
12-09 10:35:44.200: E/AndroidRuntime(24663): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-09 10:35:44.200: E/AndroidRuntime(24663): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.app.Activity.setContentView(Activity.java:1929)
12-09 10:35:44.200: E/AndroidRuntime(24663): at com.example.guidebook.GuidebookActivity.onCreate(GuidebookActivity.java:12)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.app.Activity.performCreate(Activity.java:5231)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-09 10:35:44.200: E/AndroidRuntime(24663): ... 11 more
12-09 10:35:44.200: E/AndroidRuntime(24663): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.google.android.gms.maps.SupportMapFragment that is not a Fragment
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.app.Fragment.instantiate(Fragment.java:585)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.app.Fragment.instantiate(Fragment.java:561)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.app.Activity.onCreateView(Activity.java:4778)
12-09 10:35:44.200: E/AndroidRuntime(24663): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
12-09 10:35:44.200: E/AndroidRuntime(24663): ... 20 more
12-09 10:35:44.200: E/AndroidRuntime(24663): Caused by: java.lang.ClassCastException
12-09 10:35:44.200: E/AndroidRuntime(24663): ... 24 more
12-09 10:36:10.520: D/AndroidRuntime(24870): Shutting down VM
12-09 10:36:10.520: W/dalvikvm(24870): threadid=1: thread exiting with uncaught exception (group=0x415d5ba8)
12-09 10:36:10.520: E/AndroidRuntime(24870): FATAL EXCEPTION: main
12-09 10:36:10.520: E/AndroidRuntime(24870): Process: com.example.guidebook, PID: 24870
12-09 10:36:10.520: E/AndroidRuntime(24870): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.guidebook/com.example.guidebook.GuidebookActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.os.Handler.dispatchMessage(Handler.java:102)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.os.Looper.loop(Looper.java:136)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-09 10:36:10.520: E/AndroidRuntime(24870): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 10:36:10.520: E/AndroidRuntime(24870): at java.lang.reflect.Method.invoke(Method.java:515)
12-09 10:36:10.520: E/AndroidRuntime(24870): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-09 10:36:10.520: E/AndroidRuntime(24870): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-09 10:36:10.520: E/AndroidRuntime(24870): at dalvik.system.NativeStart.main(Native Method)
12-09 10:36:10.520: E/AndroidRuntime(24870): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-09 10:36:10.520: E/AndroidRuntime(24870): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.app.Activity.setContentView(Activity.java:1929)
12-09 10:36:10.520: E/AndroidRuntime(24870): at com.example.guidebook.GuidebookActivity.onCreate(GuidebookActivity.java:12)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.app.Activity.performCreate(Activity.java:5231)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-09 10:36:10.520: E/AndroidRuntime(24870): ... 11 more
12-09 10:36:10.520: E/AndroidRuntime(24870): Caused by: java.lang.NullPointerException: name == null
12-09 10:36:10.520: E/AndroidRuntime(24870): at java.lang.VMClassLoader.findLoadedClass(Native Method)
12-09 10:36:10.520: E/AndroidRuntime(24870): at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:350)
12-09 10:36:10.520: E/AndroidRuntime(24870): at java.lang.ClassLoader.loadClass(ClassLoader.java:487)
12-09 10:36:10.520: E/AndroidRuntime(24870): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.app.Fragment.instantiate(Fragment.java:583)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.app.Fragment.instantiate(Fragment.java:561)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.app.Activity.onCreateView(Activity.java:4778)
12-09 10:36:10.520: E/AndroidRuntime(24870): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
12-09 10:36:10.520: E/AndroidRuntime(24870): ... 20 more
12-09 10:36:12.420: I/Process(24870): Sending signal. PID: 24870 SIG: 9

Answer

Check my answer below :

In your xml file change this class="com.google.android.gms.maps.MapFragment" to android:name="com.google.android.gms.maps.SupportMapFragment"

Extend your activity to FragmentActivty and change

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) .getMap(); to
map= ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map)).getMap();

Also check if (map== null) { map= ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map)).getMap();}

Check your manifest file :

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

<permission
    android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE" />

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

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<!-- Requires OpenGL ES version 2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name="com.example.googlemapsv2.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <!-- Google API Key -->
    <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="your Google map API key" />
</application>

Hope this helps.

Comments