onResume得到了错误

时间:2017-01-19 08:04:18

标签: java android onresume

我有一个情况。在StartActivity.java onCreate方法上,我将检查用户是否打开了他们的GPS。如果GPS已关闭,则用户需要打开并进入设置。因此activity将是onPause。当用户返回应用时,它将通过onResume

然后它将移动到另一个名为MainActivity.java的活动。在此活动中将生成地图。

StartActivity.java

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        if (Utils.isGPSEnabled(this)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(PERMISSIONS_LOCATION, REQUEST_LOCATION);
            } else {
                proceed();
            }
        } else {
            Utils.T(this, "Please enable your GPS location");
            Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(viewIntent);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        proceed();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode){
            case REQUEST_LOCATION :
                if (Utils.permissionGranted(requestCode, REQUEST_LOCATION, grantResults, 1)){
                    proceed();
                } else {
                    Utils.t(this, "Go to settings and enable permissions");
                }
        }
    }


    public void proceed() {
    new CountDownTimer(3000, 100) {
        public void onTick(long millisUntilFinished) {
            if (Math.round((float)millisUntilFinished / 1000.0f) != countTime) {
                countTime = Math.round((float) millisUntilFinished / 1000.0f);
            }
        }
        public void onFinish() {
            tv_detect.setText("Done!");
            intent = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(intent);
            finish();
        }
    }.start();
}

MainActivity.java

public class MainActivity extends AppCompatActivity implements
        OnMapReadyCallback, LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        View.OnClickListener, GoogleMap.OnMarkerClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buildGoogleApiClient();
    }

    protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();
   }


   @Override
   protected void onPause() {
    super.onPause();
    // stop location updates when Activity is no longer active
    if (mGoogleApiClient != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
       }
   }

  // The rest of method is already implemented
}

错误:

  

无法暂停活动   {} xx.xxx.com.xxx/xx.xxx.com.xxx.ui.activity.MainActivity:   java.lang.IllegalStateException:GoogleApiClient尚未连接。

问题:当我将proceed()方法放在onResume上时会发生错误,并且它会生成两次MainActivity.java两次。当我从proceed()移除onResume时,它会正常工作,但我需要使用onResume,因为当活动暂停时(用户转到设置以打开GPS)并返回对应用程序来说,它会卡住并且不做任何不方便的事情。

非常感谢任何帮助。感谢

2 个答案:

答案 0 :(得分:0)

您需要检查是否在MainActivity.java的onPause()中连接了mGoogleApiClient 所以你应该在onPause()

中这样做
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
   }
}

答案 1 :(得分:0)

在onResume()中检查这个条件,因为第一次创建StartActivity.java时它会调用onResume(),当从设置中返回时,它会第二次调用onResume。

if (Utils.isGPSEnabled(this)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(PERMISSIONS_LOCATION, REQUEST_LOCATION);
        } else {
            proceed();
        }
    } else {
        Utils.T(this, "Please enable your GPS location");
        Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(viewIntent);
    }