Android应用程序与Google Maps Object崩溃

时间:2017-06-15 15:04:59

标签: java android xml android-studio android-manifest

我已在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>

但是当我在手机或模拟器上运行应用程序时,应用程序崩溃了。 Android-Studio没有显示任何错误或错误。任何人都可以告诉我,错误是什么?

2 个答案:

答案 0 :(得分:2)

karte.xmlSupportMapFragment您正在使用Karte.javaSupportFragmentManager中未使用SupportMapFragment

要了解您正在使用的onCreate(),请在SupportMapFragment supportMapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); supportMapFragment.getMapAsync(this); 中尝试此操作:

static

答案 1 :(得分:1)

你在使用的布局文件中如何解释Kevinn O'Neil “SupportMapFragment”但是在java文件中没有。在.java中替换:

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

使用:

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

还有.xml文件(Android Manifestr和布局文件),你没有写下开头行:

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

将此行添加到.xml结尾的所有文件中。 我希望我能帮到你。