0

i started to inform me about google maps v2 und so i wanted to get it in my actual app!

in manifest and xml file i did everything (this works) but my problem is in my maps activity! when i click on the button where my app should starts, there only is a black screen and my project crashs :( here the code:

manifest:

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

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

    <uses-feature
                android:glEsVersion="0x00020000"
                android:required="true"/>
    <meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my api debug key (v2) " />

    <permission android:name="at.android.kioe.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="at.android.kioe.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" />



    <application
        android:icon="@drawable/label_kulinarik"
        android:label="@string/app_name" >

    <uses-library android:name="com.google.android.maps"> </uses-library>


 <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

 <activity
        android:name=".DataBaseHelper"
           android:theme="@android:style/Theme.NoTitleBar"
             />
        <activity android:name=".hashmap"
           android:theme="@android:style/Theme.NoTitleBar"
            />
        <activity android:name=".LocationDemo2"
           android:theme="@android:style/Theme.NoTitleBar"
           />

            />
        <activity android:name=".Notepadv1"
            android:theme="@android:style/Theme.NoTitleBar"
            />
   </application>

</manifest>

main.xml (mapslayout):

<?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"
    android:gravity="bottom"
    android:orientation="vertical"
    android:padding="6dp" >

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

        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />


    <FrameLayout
        android:id="@+id/frameLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/frameLayout1"
        android:layout_alignLeft="@+id/frameLayout1"
        android:layout_alignRight="@+id/frameLayout1" >

    </FrameLayout>

    <FrameLayout
        android:id="@+id/frameLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" >

        <LinearLayout
            android:id="@+id/navi"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom|center" >

            <Button
                android:id="@+id/button1"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:layout_marginTop="3dp"
                android:background="@color/black"
                android:drawableTop="@drawable/idee"
                android:onClick="StartseiteKlick"
                android:text="Idee"
                android:textColor="@color/white"
                android:textSize="5pt" />

            <Button
                android:id="@+id/button2"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:layout_marginTop="3dp"
                android:background="@color/black"
                android:drawableTop="@drawable/betriebe"
                android:onClick="BezirkeKlick"
                android:text="Betriebe"
                android:textColor="@color/white"
                android:textSize="5pt" />

            <Button
                android:id="@+id/button3"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:layout_marginTop="3dp"
                android:background="@color/black"
                android:drawableTop="@drawable/maps_open"
                android:onClick="GooglemapsKlick"
                android:text="Karte"
                android:textColor="@color/white"
                android:textSize="5pt" />

            <Button
                android:id="@+id/button4"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:layout_marginTop="3dp"
                android:background="@color/black"
                android:drawableTop="@drawable/suche"
                android:onClick="SucheKlick"
                android:text="Suche"
                android:textColor="@color/white"
                android:textSize="5pt" />
        </LinearLayout>
    </FrameLayout>

</RelativeLayout>

locationdemo2.java:

    package at.android.kioe;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

@SuppressLint("Override")
public class LocationDemo2 extends FragmentActivity {

  private static final String TAG = "**** "+LocationDemo2.class.getSimpleName();

  private MapController mapController;
  MapView mapView;
  GeoPoint geoPoint;
  boolean art;
  int pFeld[];
  int districtID;
  double zielLat,zielLng;  //geographische Breite,Länge als Parameter des Ziels
  private boolean DEBUG = false;







  //Buttons:
  public void StartseiteKlick (View vies) {
    setContentView(R.layout.startseite);
    }
  public void GooglemapsKlick (View vies) {
      setContentView(R.layout.googlemaps);
          } 
  public void SucheKlick (View vies) {
        setContentView(R.layout.suche);
    }
  public void BezirkeKlick (View vies) {
        setContentView(R.layout.bezirke);
  }

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

    Intent in = getIntent();
    art = in.getBooleanExtra("karte", false);
    if (art) { //Karte mit allen Betrieben des Bezirks
      districtID=in.getIntExtra("districtID",0);
      pFeld = in.getIntArrayExtra("pFeld"); //Koordinaten der Betriebe
    } else { //Karte mit dem aus der Liste gewählten Betrieb im Mittelpunkt
      zielLat = in.getFloatExtra("lat",0);
      zielLng = in.getFloatExtra("lng",0);
      geoPoint = new GeoPoint((int)(zielLat*1E6), (int)(zielLng*1E6));
    }
    // Zoom aktivieren

    // MapController ermitteln
    mapController = mapView.getController();
  } //Ende onCreate

  @Override
  protected void onStart() {
    super.onStart();
    if (!art) { //Karte nur für aus der Liste ausgewählten Betrieb
      //fügt der MapView neues Overlay (zum zeichnen der 'current location') hinzu
      List<Overlay> overlays = mapView.getOverlays();
//    overlays.clear();
      overlays.add(new MyOverlay());
    } else //alle Betriebe in Karte zeichnen
      multipleItems();
  }

  private void multipleItems() {
//      mapView.setSatellite(true);

    Drawable markerDefault = this.getResources().getDrawable(R.drawable.mm_20_green);
    MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(markerDefault);

    //für alle Betriebe ein Item aus den Koordinaten bilden und die
    //where-Klausel mit der _id des Betriebes hinzufügen für onTap
    for (int i=0; i<pFeld.length; i+=3) {
      if (pFeld[i+1] > 0)
      itemizedOverlay.addOverlayItem(pFeld[i+1],pFeld[i+2],"_id="+pFeld[i]);
    }
    //alle Items zur MapView hinzufügen
    mapView.getOverlays().add(itemizedOverlay);
    //den Mittelpunkt für den ausgewählten District heraussuchen
    int lat=0,lng=0;
    for (int i=0; i < Notepadv1.mpFeld.length; i+=3) {
      if (Notepadv1.mpFeld[i] == districtID) {
        lat = Notepadv1.mpFeld[i+1];
        lng = Notepadv1.mpFeld[i+2];
        //Darstellung Mittelpunkt
   //       itemizedOverlay.addOverlayItem(lat,lng,"",this.getResources().getDrawable(R.drawable.marker_default));
        mapController.setCenter(new GeoPoint(lat,lng));
        break;
      }
    }
    if (DEBUG) {
      String message="Anzahl Koord.:"+pFeld.length/3+", Mitte:"+lat+","+lng;
      Log.i(TAG,message);
      Toast.makeText(LocationDemo2.this, message, Toast.LENGTH_LONG).show();
    }
    mapController.zoomToSpan(itemizedOverlay.getLatSpanE6(), itemizedOverlay.getLonSpanE6());
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
  }

  protected boolean isRouteDisplayed() {
    return false;
  }

  /* a private inner class which extends Overlay and override the draw method
   * http://androidcookbook.com/Recipe.seam?recipeId=1541
   * zeichnet als 'overlay' den 'geoPoint' in die Karte
   */
  private class MyOverlay extends com.google.android.maps.Overlay {
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
      super.draw(canvas, mapView, shadow);
      if (!shadow && geoPoint != null) {
          mapController.setCenter(geoPoint);
          Point point = new Point(); //wird Koordinate des GeoPoint
          mapView.getProjection().toPixels(geoPoint, point);
          Bitmap bmp = BitmapFactory.decodeResource(getResources(),//R.drawable.marker_default);
                  R.drawable.mm_20_green);
          // Shift it left so the center of the image is aligned with the x-coordinate of the geo point
          int x = point.x - bmp.getWidth() / 2;
          // Shift it upward so the bottom of the image is aligned with the y-coordinate of the geo point
          int y = point.y - bmp.getHeight();
          canvas.drawBitmap(bmp, x, y, null);
      }
    }
  }

  private class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
    private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    public MyItemizedOverlay(Drawable defaultMarker) {
      super(boundCenterBottom(defaultMarker));
    }
    public void addOverlayItem(int lat, int lon, String title, Drawable altMarker) {
      GeoPoint point = new GeoPoint(lat, lon);
      OverlayItem overlayItem = new OverlayItem(point, title, null);
      addOverlayItem(overlayItem, altMarker);
    }
    public void addOverlayItem(int lat, int lon, String title) {
      GeoPoint point = new GeoPoint(lat, lon);
      OverlayItem overlayItem = new OverlayItem(point, title, null);
      addOverlayItem(overlayItem);
    }
    public void addOverlayItem(OverlayItem overlayItem) {
      mOverlays.add(overlayItem);
      populate();
    }
    public void addOverlayItem(OverlayItem overlayItem, Drawable altMarker) {
      overlayItem.setMarker(boundCenterBottom(altMarker));
      addOverlayItem(overlayItem);
    }
    @Override
    protected OverlayItem createItem(int i) {
      return mOverlays.get(i);
    }
    @Override
    public int size() {
      return mOverlays.size();
    }
    @Override
    protected boolean onTap(int index) {
      if (DEBUG)
        Toast.makeText(LocationDemo2.this,
          getItem(index).getTitle()+", Index:"+index, Toast.LENGTH_LONG).show();
      Intent in = new Intent(getApplicationContext(),Notepadv1.class);
      in.putExtra("art", "where");
      //_id des gewählten Listenpunktes
      in.putExtra("where", getItem(index).getTitle());

      //Info-Daten zum gewählten Betrieb anzeigen
      String infotext = new hashmap().texte.get(index + 1);
      new AlertDialogManager().showAlertDialog(LocationDemo2.this, "info", infotext, in);
      return true;


    }



    public class AlertDialogManager 
    {
        public void showAlertDialog(Context context, String title, String infoText, final Intent in) 
        {
            final AlertDialog alertDialog = new AlertDialog.Builder(context).create();

            // Setting Dialog Title
            alertDialog.setTitle(title);

            // Setting Dialog Message
            alertDialog.setMessage(infoText);

            alertDialog.setButton("Info", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) 
                {
                    startActivity(in);
                }
            });

            alertDialog.setButton2("zurück", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) 
                {
                    alertDialog.cancel();
                }
            });

            // Showing Alert Message
            alertDialog.show();
        }
    }

  }
  public void finish() {}
  public void finishAffinity() {}
}

logcat:

    08-31 17:05:08.981: E/AndroidRuntime(22481): FATAL EXCEPTION: main
08-31 17:05:08.981: E/AndroidRuntime(22481): java.lang.RuntimeException: Unable to start activity ComponentInfo{at.android.kioe/at.android.kioe.LocationDemo2}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.ActivityThread.access$700(ActivityThread.java:152)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.os.Looper.loop(Looper.java:137)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.ActivityThread.main(ActivityThread.java:5328)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at java.lang.reflect.Method.invokeNative(Native Method)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at java.lang.reflect.Method.invoke(Method.java:511)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at dalvik.system.NativeStart.main(Native Method)
08-31 17:05:08.981: E/AndroidRuntime(22481): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.Activity.setContentView(Activity.java:1928)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at at.android.kioe.LocationDemo2.onCreate(LocationDemo2.java:67)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.Activity.performCreate(Activity.java:5250)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-31 17:05:08.981: E/AndroidRuntime(22481):    ... 11 more
08-31 17:05:08.981: E/AndroidRuntime(22481): 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
08-31 17:05:08.981: E/AndroidRuntime(22481):    at maps.ag.bb.a(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at maps.ag.bb.a(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at maps.ag.an.a(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at maps.ag.bi.a(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at maps.ag.bh.a(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at blf.onTransact(SourceFile:107)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.os.Binder.transact(Binder.java:310)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.google.android.gms.internal.bh$4.b(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.google.android.gms.internal.bh.a(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.google.android.gms.internal.bh.onCreateView(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)

i hope that anybody could help me.. i am already searching for hours...

user2735706
  • 1
  • 1
  • 2

3 Answers3

0

Without error log, it's difficult to fix the issue.
Here is an example. Getting Started with Google MapV2 for Android Apps.

Note : The above is from my own blog.

VenomVendor
  • 15,064
  • 13
  • 65
  • 96
0

Your meta tag must be a child of application tag.

You need to extend Standard Activity instead of FragmentActivity since you min sdk is not 11 or below

You should use a MapFrament instead of SupportMapFragment.

Remove <uses-library android:name="com.google.android.maps"> </uses-library> from manifest

Make sure you follow all the steps @

https://developers.google.com/maps/documentation/android/start

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • i changed the things you told me, but same problem, i've did everithing which is at the google devlopers explained – user2735706 Aug 31 '13 at 15:50
  • @user2735706 if you followed all the steps then it should work. you might be missing or skipping few steps. you have multiple setContentView in a single activity which is not good. I don't see where you initialize GoogleMap object any where in your code. i guess you are still following the map api v1 style – Raghunandan Aug 31 '13 at 15:53
  • @user2735706 you don't need this ` ` and follow the steps in the docs and change your activity code – Raghunandan Aug 31 '13 at 15:55
  • I followed all the steps at that link too, and it didn't help me either:( It took instead a great deal of flailing for me to get the library project for Google Play Services to install correctly (at first, it would not even appear as an option in the dialog to install the Library) and get android-support-v4.jar installed. Once I did all this, the error "Unknown Source" disappeared. The usual cause of this is that the APK does not include the library: the OP may have to move the library by hand into the libs/ directory to fix this. – Matt J. Sep 01 '13 at 01:36
  • @MattJ. no need to move the library to libs. Google play services is a library project which should be referenced in your android map project. Should no add the jar to your libs folder. – Raghunandan Sep 01 '13 at 01:38
0

Haven't you read the stack trace? It says clearly:

API key not found. Check that is in the element of AndroidManifest.xml

The API Key you use ("my api debug key (v2) ") in your Manifest file is not valid. You have to create one in the API Console and use that one.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • the api key ie the meta tag is not under application tag. he has the key but metag is not under application tag. the stack trace says Check that is in the element of AndroidManifest.xml 08-31 17:05:08.981: E/AndroidRuntime(22481): at maps.ag.bb.a(Unknown Source) – Raghunandan Sep 01 '13 at 01:42