Java 为什么在android的oncreate methode中没有其他活动?

Java 为什么在android的oncreate methode中没有其他活动?,java,if-statement,Java,If Statement,我试图手动升级google play服务,但总是在语句激活时升级。 我用两台设备进行测试。在第一台设备中,google play services小于10的版本对话框显示正确,但在google play services升级的第二台设备中,应用程序在执行后崩溃 @Override protected void onCreate(Bundle savedInstanceState) { if (checkPlayServices(ActivityMap.this) == true) {

我试图手动升级google play服务,但总是在语句激活时升级。 我用两台设备进行测试。在第一台设备中,google play services小于10的版本对话框显示正确,但在google play services升级的第二台设备中,应用程序在执行后崩溃

@Override
protected void onCreate(Bundle savedInstanceState) {

    if (checkPlayServices(ActivityMap.this) == true) {
        dialog.show();
    } else {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        shopOnMap();

        if (savedInstanceState != null) {
            mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
            mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
        }

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */,
                        this /* OnConnectionFailedListener */)
                .addConnectionCallbacks(this)
                .addApi(LocationServices.API)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .build();
        mGoogleApiClient.connect();

    }
}


 public boolean checkPlayServices(Activity activity) {
    boolean res= false;
    GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
    int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity);
    if (resultCode != ConnectionResult.SUCCESS) {
        AlertDialog.Builder builder = new AlertDialog.Builder(ActivityMap.this);

        builder.setMessage("Do you want update Google play services?")
                .setTitle("Google Play Services"); 

        builder.setPositiveButton("ِDownload", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

                new DownloadNewVersion().execute();

            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
            }
        });
        dialog = builder.create();
      res=true;
    }
    return res;

}

答案很简单-因为您总是返回
true
作为此函数的返回值。唯一的
return
语句是
returntrue

我相信您在代码中处理错误。有很多地方可以改进,但我只强调我可以立即在代码中看到的重要内容

正如@PeterJ所提到的,这是您在代码中犯的逻辑错误。您在
checkPlayServices()
中返回
true
,只有当您的条件
为(resultCode!=ConnectionResult.SUCCESS)
为true时,才应将其更正为返回
true
,否则应返回
false
,以便在这种情况下不会显示对话框

现在来谈谈你对撞车的第二个担忧。我相信这是因为您正在调用
super.onCreate(savedInstanceState)仅在您的
else
条件下

派生类onCreate(bundle)方法必须调用此方法的超类实现。如果不这样做,则会引发异常SuperNotCalledException


若要解决此问题,请将此语句移动到
onCreate()
函数的顶部,希望这能解决由它引起的崩溃问题

当询问有关代码的问题时,尤其是有关上下文和语义敏感的问题,如
if
语句和条件时,请始终将编写代码的语言作为标记。下次别忘了。哇,谢谢你的回答。如何更改if/else条件正常工作的代码?这是您的函数我如何知道何时需要返回false我希望在else中返回false,但我的代码也不工作。