Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Implementing maps v2 to navigation drawer

I tried to merge two android demo programs, but I couldn't get it right.

I have tried and searched for several ways, but I couldn't figure it out. Could you please help me?

Here's my error message:

09-20 03:28:25.617: E/AndroidRuntime(20108): FATAL EXCEPTION: main
09-20 03:28:25.617: E/AndroidRuntime(20108): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.navigation/com.example.android.navigation.MainActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.os.Looper.loop(Looper.java:137)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.ActivityThread.main(ActivityThread.java:5041)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at java.lang.reflect.Method.invokeNative(Native Method)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at java.lang.reflect.Method.invoke(Method.java:511)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at dalvik.system.NativeStart.main(Native Method)
09-20 03:28:25.617: E/AndroidRuntime(20108): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.example.android.navigation.BasicMapActivity.onCreateView(BasicMapActivity.java:57)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:925)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1458)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.Activity.performStart(Activity.java:5114)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
09-20 03:28:25.617: E/AndroidRuntime(20108):    ... 11 more
09-20 03:28:25.617: E/AndroidRuntime(20108): Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
09-20 03:28:25.617: E/AndroidRuntime(20108):    at maps.ag.bb.a(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at maps.ag.bb.a(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at maps.ag.an.a(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at maps.ag.bi.a(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at maps.ag.bh.a(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at blf.onTransact(SourceFile:107)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.os.Binder.transact(Binder.java:310)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.google.android.gms.internal.bh$4.b(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.google.android.gms.internal.bh.a(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.google.android.gms.internal.bh.onCreateView(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:898)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1182)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
09-20 03:28:25.617: E/AndroidRuntime(20108):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
09-20 03:28:25.617: E/AndroidRuntime(20108):    ... 23 more
09-20 03:30:16.829: D/dalvikvm(20329): GC_CONCURRENT freed 189K, 3% free 8774K/9016K, paused 1ms+0ms, total 3ms
09-20 03:30:17.005: D/AndroidRuntime(20329): Shutting down VM
09-20 03:30:17.009: W/dalvikvm(20329): threadid=1: thread exiting with uncaught exception (group=0xa617f908)
09-20 03:30:17.025: E/AndroidRuntime(20329): FATAL EXCEPTION: main
09-20 03:30:17.025: E/AndroidRuntime(20329): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.navigation/com.example.android.navigation.MainActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.os.Looper.loop(Looper.java:137)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.ActivityThread.main(ActivityThread.java:5041)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at java.lang.reflect.Method.invokeNative(Native Method)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at java.lang.reflect.Method.invoke(Method.java:511)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at dalvik.system.NativeStart.main(Native Method)
09-20 03:30:17.025: E/AndroidRuntime(20329): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.example.android.navigation.BasicMapActivity.onCreateView(BasicMapActivity.java:57)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:925)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1458)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.Activity.performStart(Activity.java:5114)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
09-20 03:30:17.025: E/AndroidRuntime(20329):    ... 11 more
09-20 03:30:17.025: E/AndroidRuntime(20329): Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
09-20 03:30:17.025: E/AndroidRuntime(20329):    at maps.ag.bb.a(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at maps.ag.bb.a(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at maps.ag.an.a(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at maps.ag.bi.a(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at maps.ag.bh.a(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at blf.onTransact(SourceFile:107)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.os.Binder.transact(Binder.java:310)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.google.android.gms.internal.bh$4.b(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.google.android.gms.internal.bh.a(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.google.android.gms.internal.bh.onCreateView(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:898)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1182)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
09-20 03:30:17.025: E/AndroidRuntime(20329):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
09-20 03:30:17.025: E/AndroidRuntime(20329):    ... 23 more

Here's my source code, I only changed this function:

private void selectItem(int position) {
    // update the main content by replacing fragments

    Fragment mapfragment = new BasicMapActivity();

    Fragment fragment = new PlanetFragment();
    Bundle args = new Bundle();
    args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
    fragment.setArguments(args);

    mapfragment.getFragmentManager();
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, mapfragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

Here's my map fragment:

public class BasicMapActivity extends Fragment {
/**
 * Note that this may be null if the Google Play services APK is not available.
 */
private GoogleMap mMap;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setUpMapIfNeeded();
    //setContentView(R.layout.basic_demo);

}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    //setUpMapIfNeeded();
     View rootView = inflater.inflate(R.layout.basic_demo, container, false);
     setUpMapIfNeeded();
     return rootView;
}

@Override
public void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}

Also the xml file for BasicMapActivity:

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

EDIT:

Here is the main program's XML:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     The drawer is given a fixed width in dp and extends the full height of
     the container. A solid background is used for contrast
     with the content view. -->
<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

As long as my manifest:

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

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

<permission
android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE"/>
<!-- Copied from Google Maps Library/AndroidManifest.xml. -->
<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"/>
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- My Location -->
<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"/>
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDJbPX63ZJni6sbzNxaFvFUCnYLb7-9o28"/>

<application
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
    android:allowBackup="true">


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

</application>

</manifest>

Could anyone give me a hint? I have been jammed here for 2-3 days.

Thank you so much!

like image 662
Sherwood Lee Avatar asked Sep 20 '13 08:09

Sherwood Lee


3 Answers

Your issu is simple

The API KEY must be inside the Application tag, also you should put it at the end

Example :

<application
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
    android:allowBackup="true">

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

    <meta-data android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyDJbPX63ZJni6sbzNxaFvFUCnYLb7-9o28"/>

</application>
like image 124
An-droid Avatar answered Nov 20 '22 01:11

An-droid


Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data  
android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/>  
is in the <application> element of AndroidManifest.xml

Get a valid API key. With out this you can't run a map V2 in your App. Here's the doc how you can get one.

Another error occurs in your xml layout for the BasicMapActivity at line #18. Please post that XML in your question.

like image 28
Steve Benett Avatar answered Nov 20 '22 03:11

Steve Benett


In the error message you had two problems:

1.- "android.view.InflateException: Binary XML file line #18: Error inflating class fragment"

2.- "java.lang.RuntimeException: API key not found. Check that is in the element of AndroidManifest.xml"

The solution offered by "yume117" resolved the number 2, but I think that the problem "inflating class fragment" is due to the xml file for BasicMapActivity.

Instead of a "fragment" inside your "layout" you would need to change to something like this:

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

Also you would have to change the java class. In this url is an example for controlling this (is in spanish but I think that the code is comprehensible): https://groups.google.com/forum/#!msg/desarrolladores-android/1cvqPm0EZZU/9srw_9feamUJ

like image 1
Ignacio Rubio Avatar answered Nov 20 '22 03:11

Ignacio Rubio