【问题标题】:Android app crashing every second time in lollipop onlyAndroid 应用程序仅在棒棒糖中每秒崩溃一次
【发布时间】:2016-02-23 10:47:54
【问题描述】:

我是 Android 工作室的新手,我正在开发一款基于位置的应用。在这里,我需要在某些情况下关闭应用程序,所以我调用了 finish();函数并在 onDestroy() 中杀死进程

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

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");
                              }
                          }
    );
}

【问题讨论】:

  • 发布您的崩溃日志,其中应该包含有关崩溃的信息...
  • 你为什么还要使用Process.killProcess(Process.myPid());finish() 本身关闭活动。
  • 我不知道为什么它不起作用,但您可以尝试调用 'System.exit(0)' 来代替它,这是关闭它的好方法
  • 鞋履崩溃记录
  • @DavidPeicho 你不应该在他试图学习的时候开玩笑。正如 Rohit 所说,Surya 应该没有理由杀死你的 pid。你想达到什么目的?

标签: android kill-process


【解决方案1】:

好吧,您不需要终止该进程。调用 finish() 就足够了;它将通过其自然生命周期销毁当前活动,如果它是最后一个活动,应用程序将有效退出。

更多关于为什么不应该朝那个方向发展的讨论: Is quitting an application frowned upon?

问候!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多