getMapAsync错误

时间:2017-06-15 00:56:20

标签: java android google-maps android-fragments

当我试图调用它时,我收到错误'无法解析符号getMapAsync'。我在其他帖子中读过,但他们的代码也像我的。所以这就是:

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;

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

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


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

////////////////////////////// LAYOUT //////////////////////////////
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.karte);

    //// UP BOOTON ////

    // Get a support ActionBar corresponding to this toolbar
    ActionBar ab = getSupportActionBar();

    // Enable the Up button
    ab.setDisplayHomeAsUpEnabled(true);

    //////////////////////////////////////////////
    //////////////// 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");

}

@Override
public void onMapReady(GoogleMap map) {
    this.m_map = 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 个答案:

答案 0 :(得分:2)

那是因为你应该使用SupportMapFragment。修改xml片段代码:

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

SupportMapFragment mapFragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);

然后你就可以打电话了:

mapFragment.getMapAsync(this);

答案 1 :(得分:2)

您在声明中使用以下代码而不是方法:

    mapFragment.getMapAsync(this);

尝试将其放入onCreate()方法中。