【问题标题】:getMapAsync ErrorgetMapAsync 错误
【发布时间】:2017-06-15 00:56:19
【问题描述】:

我在尝试调用它时收到错误“无法解析符号 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);
}

}

如果有人可以回答或知道是什么问题,请。

【问题讨论】:

    标签: java android google-maps android-fragments


    【解决方案1】:

    您在声明中包含以下代码而不是方法:

        mapFragment.getMapAsync(this);
    

    试着把它放在你的 onCreate() 方法中。

    【讨论】:

      【解决方案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);

      【讨论】:

      • 我做到了,但 getMapAsync 仍然是红色突出显示,还有 getChildFragmentManager
      猜你喜欢
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 2015-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多