BAR Software
BAR Software

Reputation: 163

Android application crashes with Google Maps Object

I have programmed this code in Android Studio:

1) Karte.java

package barsoftware.suedtirolpointer;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

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.OnMapReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class Karte extends AppCompatActivity implements OnMapReadyCallback {

GoogleMap m_map;
boolean mapReady=false;

MarkerOptions Rieserfernerhütte;

MarkerOptions Dahoam;


static final CameraPosition SÜDTIROL = CameraPosition.builder()
        .target(new LatLng(46.470576, 11.339986))
        .zoom(8)
        .bearing(0)
        .tilt(0)
        .build();


@Override
public Resources getResources() {
    return super.getResources();
}

////////////////////////////// onCreate //////////////////////////////
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //// LAYOUT ////

    setContentView(R.layout.karte);

    //// KARTEN POI'S ////
    Rieserfernerhütte = new MarkerOptions()
            .position(new LatLng(46.5324, 12.0446))
            .title("Rieserfernerhütte");

    Dahoam = new MarkerOptions()
            .position(new LatLng(46.738886, 12.166471))
            .title("Dahoam");

    //// KARTEN IMPLEMENTIERUNG ////
    MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    //// UP BOOTON ////

    ActionBar ab = getSupportActionBar();
    ab.setDisplayHomeAsUpEnabled(true);

}

@Override
public void onMapReady(GoogleMap map) {
    mapReady = true;
    m_map = map;
    m_map.addMarker(Rieserfernerhütte);
    m_map.addMarker(Dahoam);
    flyTo(SÜDTIROL);
}

private void flyTo(CameraPosition target)
{
    m_map.moveCamera(CameraUpdateFactory.newCameraPosition(target));
}




////////////////////////////////// MENU //////////////////////////////////
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_no_karte, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.menu_home) {
        Intent Home = new Intent(Karte.this,
                Start.class);
        startActivity(Home);
    }

    if (id == R.id.menu_karte) {

    }

    if (id == R.id.menu_teilnehmer) {
        Intent Teilnehmer = new Intent(Karte.this,
                Teilnehmer.class);
        startActivity(Teilnehmer);
    }

    if (id == R.id.menu_einstellungen) {
        Intent Einstellungen = new Intent(Karte.this,
                Einstellungen.class);
        startActivity(Einstellungen);
    }
    if (id == R.id.menu_update) {
        Intent Update = new Intent(Karte.this,
                Update.class);
        startActivity(Update);
    }
    if (id == R.id.menu_teilen) {
        Intent Teilen = new Intent(Karte.this,
                Teilen.class);
        startActivity(Teilen);
    }

    return super.onOptionsItemSelected(item);
  }

}

2) karte.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >

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

</RelativeLayout>

3) Android Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="barsoftware.suedtirolpointer">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission    android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission   android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-feature android:glEsVersion="0x00020000"
              android:required="true"/>


<application

    android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@drawable/icon"
    android:theme="@style/AppTheme">

    <activity
        android:name=".Start"
        android:label="@string/act_home" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Karte"
              android:label="@string/act_karte"
              android:parentActivityName=".Start"/>

    <activity android:name=".Teilnehmer"
              android:label="@string/act_teilnehmer"
              android:parentActivityName=".Start"/>

    <activity android:name=".Einstellungen"
              android:label="@string/act_einstellungen"
              android:parentActivityName=".Start"/>

    <activity android:name=".Update"
              android:label="@string/act_update"
              android:parentActivityName=".Start"/>

    <activity android:name=".Teilen"
              android:label="@string/act_teilen"
              android:parentActivityName=".Start"/>

    <activity android:name=".Permission"
              android:label="Permission"/>

    <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>

    <meta-data android:name="com.google.android.geo.API_KEY"
               android:value="AIzaSyCT_CaCyQ2uLFclTcrWex-AEmJ1aHWhR08"/>

 </application>
 </manifest>

But when I run the application on my phone or on the emulator the appliation crashes. Android-Studio is not showing any errors or bugs. Can anyone show me, what's the error?

Upvotes: 2

Views: 137

Answers (2)

Kevin O&#39;Neil
Kevin O&#39;Neil

Reputation: 1421

In karte.xml you are using the SupportMapFragment but in Karte.java you are not using the SupportFragmentManager.

To get a handle on the SupportMapFragment you are using try this in onCreate():

SupportMapFragment supportMapFragment = 
    (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(this);

Upvotes: 2

Antonio Gschossmann
Antonio Gschossmann

Reputation: 612

How just explained from Kevinn O'Neil in the layout file you are using the "SupportMapFragment" but in the java file not. In the .java replace:

MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

with:

SupportMapFragment supportMapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(this);

And also in the .xml files (Android Manifestr and layout file) you didn't wrote the beginning line:

<?xml version="1.0" encoding="utf-8"?>

Add this line to all files with .xml ending. I hope I was able to help you.

Upvotes: 1

Related Questions