Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我不总是打电话来_Java_Android_Google Maps Android Api 2 - Fatal编程技术网

Java 我不总是打电话来

Java 我不总是打电话来,java,android,google-maps-android-api-2,Java,Android,Google Maps Android Api 2,我有三项活动: 主要活动: public void start_main_map(View view) { Intent intent = new Intent(this, com.example.MainMap.class); startActivity(intent); } 主地图: protected GoogleApiClient mGoogleApiClient; @Override protected void onCreate(Bundle sav

我有三项活动:

主要活动:

public void start_main_map(View view) {
        Intent intent = new Intent(this, com.example.MainMap.class);
        startActivity(intent);
}
主地图:

protected GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "onCreate");
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
     mapFragment.getMapAsync(this);
}

@Override
protected void onStart() {
    Log.i(TAG, "onStart");
    mGoogleApiClient.connect();
    super.onStart();
}

@Override
protected void onStop() {
    Log.i(TAG, "onStop");
    mGoogleApiClient.disconnect();
    super.onStop();
}

@Override
protected void onPause() {
    Log.i(TAG, "onPause");
    super.onPause();
}

@Override
public void onResume() {
    Log.i(TAG, "onResume");
    super.onResume();
}

@Override
public void onMapReady(GoogleMap googleMap) {
    Log.i(TAG, "onMapReady");
    // do some serious work here
}
指南:

private void implement_back_button() {
    final Intent intent = new Intent(this, com.example.MainMap.class);
    back = (Button) findViewById(R.id.back_button);
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(intent);
        }
    });
}
每当我从
MainActivity
转到
MainMap
时,我会得到:

I/MainMap: onCreate
I/MainMap: onStart
I/MainMap: onResume
I/MainMap: onMapReady
然而,每当我从
Guide
转到
MainMap
(使用
back\u按钮或按手机上的back键)时,我只得到:

I/MainMap: onCreate
I/MainMap: onStart
I/MainMap: onResume

不,
onMapReady
-因此“严肃的工作”从未被提及。我不能理解这种行为。如何确保在所有情况下都调用了
onMapReady

当您从其他活动中按下后退键时,映射活动类只需继续。而且由于地图在so onMapReady之前已经加载,因此不会再次调用

要解决这个问题,只需在从Map Activity启动其他活动之前完成Map活动

像这样

Intent intent  = new Intent(MapActivity.class, OtherActivity.class);

startActivity(intent);

MapActivity.this.finish();

当您从其他活动中按下后退键时,映射活动类就可以继续了。而且由于地图在so onMapReady之前已经加载,因此不会再次调用

要解决这个问题,只需在从Map Activity启动其他活动之前完成Map活动

像这样

Intent intent  = new Intent(MapActivity.class, OtherActivity.class);

startActivity(intent);

MapActivity.this.finish();

MainActivity
MainMap

调用了
onMapReady()
,因为
MainMap
是第一次创建的

Guide
MainMap

未调用
onMapReady()
,因为您刚刚返回到
MainMap
MainMap
始终存在于活动
stack
中,并且其
Map
处于
ready
状态

解决方案:


在开始
Guide
活动之前,完成
MainMap

MainMap
MainMap

调用了
onMapReady()
,因为
MainMap
是第一次创建的

Guide
MainMap

未调用
onMapReady()
,因为您刚刚返回到
MainMap
MainMap
始终存在于活动
stack
中,并且其
Map
处于
ready
状态

解决方案:


在开始
Guide
活动之前,先完成
MainMap

完成Map活动有点笨手笨脚。 如果Google地图活动恢复,您应该能够重新使用Google地图引用

像这样的方法应该会奏效:

protected GoogleApiClient mGoogleApiClient;
protected SupportMapFragment mapFragment;
protected GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "onCreate");
    mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
protected void onStart() {
    Log.i(TAG, "onStart");
    mGoogleApiClient.connect();
    super.onStart();
}

@Override
protected void onStop() {
    Log.i(TAG, "onStop");
    mGoogleApiClient.disconnect();
    super.onStop();
}

@Override
public void onResume() {
    Log.i(TAG, "onResume");
    super.onResume();

    //Added:
    if (mMap == null) {
      mapFragment.getMapAsync(this);
    } else {
      doSomeSeriousWork();
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    Log.i(TAG, "onMapReady");
    doSomeSeriousWork();
}

public void doSomeSeriousWork() {
    // do some serious work here
}

完成地图活动有点费力。 如果Google地图活动恢复,您应该能够重新使用Google地图引用

像这样的方法应该会奏效:

protected GoogleApiClient mGoogleApiClient;
protected SupportMapFragment mapFragment;
protected GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "onCreate");
    mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
protected void onStart() {
    Log.i(TAG, "onStart");
    mGoogleApiClient.connect();
    super.onStart();
}

@Override
protected void onStop() {
    Log.i(TAG, "onStop");
    mGoogleApiClient.disconnect();
    super.onStop();
}

@Override
public void onResume() {
    Log.i(TAG, "onResume");
    super.onResume();

    //Added:
    if (mMap == null) {
      mapFragment.getMapAsync(this);
    } else {
      doSomeSeriousWork();
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    Log.i(TAG, "onMapReady");
    doSomeSeriousWork();
}

public void doSomeSeriousWork() {
    // do some serious work here
}

希望这有帮助你的问题不清楚。你的意思是如果你从主活动开始映射活动,onMapReady调用,但如果你从其他活动返回映射活动,onMapReady不调用?是的,这正是我的意思检查我的答案below@Praveen,一点帮助都没有-我已经在我的课上实现了
在mapreadycallback上
,希望这个帮助你的问题不清楚。你的意思是如果你从主活动开始映射活动,onMapReady调用,但如果你从其他活动返回映射活动,onMapReady不调用?是的,这正是我的意思检查我的答案below@Praveen,一点帮助都没有-我的类中已经有
在mapreadycallback上实现了