Problem mit GoogleMaps

N

nussecke001

Neues Mitglied
0
Hallo!

Im Rahmen einer Studienarbeit bin ich dabei mit Eclipse eine App zu programmieren. Bis jetzt funktioniert alles recht gut, allerdings möchte ich eine Map einbinden und komme nicht weiter.

Ich habe einen Google-Developer-Key erstellt, den ich in das Manifest der App eingebunden habe und eine eigene Activity, die die Map darstellen bzw. öffnen soll.
Allerdings glaube ich, dass genau da der Fehler liegt. (Ich vermute in der Java-Datei.)
Anfangs habe ich keinerlei Fehlermeldungen bekommen, aber egal womit ich die App öffne - sobald ich zu der Activity gelange, die die Map darstellt, stürzt die App ab.
Ich habe mich schon durch sämtliche Foren und Google-Seiten geklickt, aber irgendwie finde ich nicht, woran das liegen könnte.

Deshalb jetzt meine Frage: Wäre es möglich, dass einer von euch einen Blick auf den Code der App wirft?
Als Neuling kenne ich mich leider nicht gut genug aus und ich wäre wirklich total glücklich wenn mir jemand weiterhelfen könnte.

Vielen Dank im Voraus -
nussecke001
 
Code?
Logcat?

lg. Dagobert
 
Hier der Code der XML-Datei:

Code:
<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" >


    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:apiKey="AIzaSyC0wLMVJjLJPcJoL5OVflYmqoCBRVvdFEo "/>

</RelativeLayout>

und der der Java-Datei:
Code:
package com.example.altmuehltaltipps;

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

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class AltmuehlbrueckeMap extends Activity {
  static final LatLng ALTMUEHLBRUECKE = new LatLng(48.889797, 11.259096);
  private GoogleMap map;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_altmuehlbruecke_map);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();
    Marker altmuehlbruecke = map.addMarker(new MarkerOptions().position(ALTMUEHLBRUECKE)
        .title("Altmühlbrücke"));

    // Move the camera instantly to altmuehlbruecke with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(ALTMUEHLBRUECKE, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
  }


}

Während ich in der XML-Datei keine Fehlermeldung bekomme, markiert er mir in der Java-Datei folgende Zeile:

Code:
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))

Die Fehlermeldung lautet "map cannot be resolved or is not a field". Vielleicht hilft das irgendwie. Tut mir Leid falls meine Frage total banal ist, aber ich bin wirklich blutiger Anfänger und habe keine Ahnung ...
 
Zuletzt bearbeitet:
Code:
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
Du suchst hier nen nach ne id map....

hast aber vorher als id mapview vergeben..

ist doch klar das er map nicht finden kann oder?

du solltest vllt R.id.mapview verwenden...

lg.
 
Hi :)
Zwar ist jetzt der Fehler weg, aber es läuft leider immer noch nicht. Wenn ich die App mit dem Emulator oder dem Smartphone starte schließt sie sich sobald ich die Activity öffne selbst.

Außerdem wird mir die Zeile

Code:
    Marker altmuehlbruecke = map.addMarker(new MarkerOptions().position(ALTMUEHLBRUECKE)
        .title("Altmühlbrücke"));

jetzt gelb anmarkiert. (The value of the local variable altmuehlbruecke is not used)
 
Du lernst es nicht was... :D

ohne logcat keine Hilfe...

lg.
 
Tut mir Leid :D

Hier die LogCat-Meldung wenn ich versuche Maps zu öffnen:

Code:
01-21 10:47:37.674: D/AndroidRuntime(8053): Shutting down VM
01-21 10:47:37.674: W/dalvikvm(8053): threadid=1: thread exiting with uncaught exception (group=0x4180ac08)
01-21 10:47:37.684: E/AndroidRuntime(8053): FATAL EXCEPTION: main
01-21 10:47:37.684: E/AndroidRuntime(8053): Process: com.example.altmuehltaltipps, PID: 8053
01-21 10:47:37.684: E/AndroidRuntime(8053): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.altmuehltaltipps/com.example.altmuehltaltipps.AltmuehlbrueckeMap}: android.view.InflateException: Binary XML file line #8: Error inflating class com.google.android.maps.MapView
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2449)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.app.ActivityThread.access$900(ActivityThread.java:172)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.os.Looper.loop(Looper.java:146)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.app.ActivityThread.main(ActivityThread.java:5694)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at java.lang.reflect.Method.invokeNative(Native Method)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at java.lang.reflect.Method.invoke(Method.java:515)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at dalvik.system.NativeStart.main(Native Method)
01-21 10:47:37.684: E/AndroidRuntime(8053): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.google.android.maps.MapView
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.view.LayoutInflater.createView(LayoutInflater.java:626)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.app.Activity.setContentView(Activity.java:2059)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at com.example.altmuehltaltipps.AltmuehlbrueckeMap.onCreate(AltmuehlbrueckeMap.java:20)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.app.Activity.performCreate(Activity.java:5541)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
01-21 10:47:37.684: E/AndroidRuntime(8053):     ... 11 more
01-21 10:47:37.684: E/AndroidRuntime(8053): Caused by: java.lang.reflect.InvocationTargetException
01-21 10:47:37.684: E/AndroidRuntime(8053):     at java.lang.reflect.Constructor.constructNative(Native Method)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at android.view.LayoutInflater.createView(LayoutInflater.java:600)
01-21 10:47:37.684: E/AndroidRuntime(8053):     ... 22 more
01-21 10:47:37.684: E/AndroidRuntime(8053): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.
01-21 10:47:37.684: E/AndroidRuntime(8053):     at com.google.android.maps.MapView.<init>(MapView.java:291)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at com.google.android.maps.MapView.<init>(MapView.java:264)
01-21 10:47:37.684: E/AndroidRuntime(8053):     at com.google.android.maps.MapView.<init>(MapView.java:247)
01-21 10:47:37.684: E/AndroidRuntime(8053):     ... 25 more
 
Binary XML file line #8: Error inflating class com.google.android.maps.MapView

In Zeile 8 stimmt dein XML file nicht...

was für ein Tutorial hast du verwendet und wie alt ist es... am besten du arbeitest (fast) immer mit der doku von google...

die ist spitze...

lg. Dagobert
 
Code:
MapViews can only be created inside instances of MapActivity.

vs.

Code:
public class AltmuehlbrueckeMap extends Activity

Liegt da nicht ein Problem vor?
 
Das bedeutet ich muss das in
Code:
public class AltmuehlbrueckeMap extends MapActivity
ändern, oder?

Wenn ich das mache werden aber die beiden Zeilen rot markiert
Code:
    setContentView(R.layout.activity_altmuehlbruecke_map);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapview))

weil die Methoden nicht definiert sind.
Wie definiere ich die Methoden? Also was genau muss da rein? :(
Tut mir Leid, wahrscheinlich ist die Frage ziemlich dämlich ...

Der ursprüngliche Beitrag von 11:02 Uhr wurde um 11:04 Uhr ergänzt:

Und was das Tutorial betrifft: Ich habe mich an ein Tutorial von Vogella gehalten, das mir von meinem Professor empfohlen wurde.

Der ursprüngliche Beitrag von 11:04 Uhr wurde um 11:12 Uhr ergänzt:

Ich habe noch mal im Tutorial nachgesehen, und der Code von der XML-Datei wurde dort abgeändert in
Code:
 [COLOR=#008080][COLOR=#000000]    [/COLOR][FONT=Consolas, sans-serif][SIZE=2]<[COLOR=#3f7f7f]fragment[/COLOR][/SIZE][/FONT][/COLOR]
         [FONT=Consolas, sans-serif][SIZE=2][COLOR=#7f007f]android:id[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#2a00ff][I]"@+id/map"[/I][/COLOR][/SIZE][/FONT]
         [FONT=Consolas, sans-serif][SIZE=2][COLOR=#7f007f]android:layout_width[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#2a00ff][I]"match_parent"[/I][/COLOR][/SIZE][/FONT]
         [FONT=Consolas, sans-serif][SIZE=2][COLOR=#7f007f]android:layout_height[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#2a00ff][I]"match_parent"[/I][/COLOR][/SIZE][/FONT]
         [FONT=Consolas, sans-serif][SIZE=2][COLOR=#7f007f]class[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#2a00ff][I]"com.google.android.gms.maps.MapFragment"[/I][/COLOR] [COLOR=#008080]/>[/COLOR][/SIZE][/FONT]

Habe den jetzt so in meine XML-Datei übernommen ...
 
Code:
android:id="@+id/map"
oder
Code:
android:id="@+id/mapview"
:smile:?
 
Mapview, du hast Recht :) Habs geändert.

Der ursprüngliche Beitrag von 11:22 Uhr wurde um 11:32 Uhr ergänzt:

Die beiden Zeilen im Code

Code:
    setContentView(R.layout.activity_altmuehlbruecke_map);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapview))
        .getMap();
sind leider immer noch rot markiert ... Ich weiß einfach nicht was genau ich jetzt machen muss. Reicht es wenn ich einfach über Eclipse eine Methode erstellen lasse, also

Code:
private void setContentView(int activityAltmuehlbrueckeMap) {
    // TODO Auto-generated method stub
    
}
oder muss ich jetzt noch etwas beachten? Der Fehler verschwindet so zwar, aber ich weiß nicht ob das die richtige Lösung ist.
 
Die konkrete Fehlermeldung ist
"The method setContentView(int) is undefined for the Type AltmuehlbrueckeMap"
bzw.
"Multiple Markers at this line:
- The method getFragmentManager(int) is undefined for the Type AltmuehlbrueckeMap
- LineBreakpoint: AltmuehlbrueckeMap [line 22] - onCreate(Bundle)

Wegen Tutorials schau ich mal - vielen Dank euch beiden ;)
 
An Auflösungen sind wir aber auch interessiert :).

"The method setContentView(int) is undefined for the Type AltmuehlbrueckeMap"

heisst:
Übergib einen int statt einer AltmuehlbrueckeMap als Parameter...
 
Ja, danke :) Ich halte euch auf dem Laufenden.
Falls jemand 'nen Lösungsvorschlag hat wär ich sehr dankbar ;)
 
Als int wird hier die int-Id (Hex-Wert in der R-Java) gefordert.
Da man nicht weiß wie die Klasse aktuell aussieht, wird es schwierig hier dienliche Hinweise zugeben.
Am besten nochmal den Code posten.
Generell ist fraglich ob der Aufruf von setContentView an dieser Stelle korrekt ist, besser: ob die aktuelle Projektstuktur so sinnvoll ist. Letztlich lässt das (Map)Fragment auch extern* programmieren (also so, dass es nur eingehängt werden muss).

*ich weiß nicht ob "extern" hier der richtige Ausdruck ist. Gemeint ist, dass das Fragment "für sich selbst" stehen kann und somit austauschbar ist.
 
Dann poste ich am besten noch mal den aktuellen Code.

Hier die XML-Datei:
Code:
<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" >


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

</RelativeLayout>

und hier die Java-Datei:

Code:
package com.example.altmuehltaltipps;

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

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class AltmuehlbrueckeMap extends MapActivity {
  static final LatLng ALTMUEHLBRUECKE = new LatLng(48.889797, 11.259096);
  private GoogleMap map;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(0x7f030002);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapview))
        .getMap();
    Marker altmuehlbruecke = map.addMarker(new MarkerOptions().position(ALTMUEHLBRUECKE)
        .title("Altmühlbrücke"));

    // Move the camera instantly to altmuehlbruecke with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(ALTMUEHLBRUECKE, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
  }

}
(Hab also den R.layout.... gegen den Hex-Wert getauscht). Derselbe Fehler bleibt allerdings.

Du meinst also dass ich keine eigene Activity bräuchte, sondern in meiner Activity, die eigentlich zuvor durchlaufen wird das Fragment einbauen kann?
 
Wie heisst Deine Layout-Datei eigentlich?

activity_altmuehlbruecke_map.xml?
 
Ja genau
 

Ähnliche Themen

S
Antworten
4
Aufrufe
1.026
Sempervivum
S
B
Antworten
4
Aufrufe
526
bb321
B
R
Antworten
3
Aufrufe
1.668
Ritartet
R
Zurück
Oben Unten