Android应用程序仅在棒棒糖中每秒崩溃一次



我是Android studio的新手,我正在开发一个基于位置的应用程序。在这里,我需要在某个实例上关闭应用程序,所以我调用了 finish();函数并杀死了 onDestroy() 中的进程

问题:在android 4.4(奇巧)中一切正常,但在棒棒糖中崩溃(安装后第二次打开应用程序时崩溃)

public void onClick(){
    finish();
}
@Override                   //------------after the finish(); called---//
protected void onDestroy() {
    Process.killProcess(Process.myPid());
    super.onDestroy();
}
@Override
public void onBackPressed() {
    super.onBackPressed();
}
@Override
protected void onPause() {
    super.onPause();
}    
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    from = (EditText) findViewById(R.id.from);
    to = (EditText) findViewById(R.id.to);
    go = (Button) findViewById(R.id.go);
    resulted = (TextView) findViewById(R.id.result);
    time = (TextView) findViewById(R.id.time1);
    button = (Button) findViewById(R.id.button);

    android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);
     audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    pb = (ProgressBar) findViewById(R.id.progressBar1);
    pb.setVisibility(View.INVISIBLE);
    locationManager =            (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    go.setOnClickListener(new View.OnClickListener() {
                              public void onClick(View v) {
                                  str_from1=from.getText().toString();
                                  str_to1=to.getText().toString();
                                             str_from1 = str_from1.replaceAll("[^\w]+", "+");
                                              str_to1 = str_to1.replaceAll("[^\w]+", "+");
                                              new JSONTask().execute("https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + str_from1 + "&destinations=" + str_to1 + "&mode=driving&language=fr-FR&key=API KEY");
                              }
                          }
    );
}

好吧,你不需要杀死这个过程。调用 finish() 应该就足够了;它将在其自然生命周期中破坏当前活动,如果它是最后一个活动,应用程序将有效地退出。

有关为什么您不应该朝这个方向前进的更多讨论:退出申请是不是不受欢迎的?

问候!

最新更新