【发布时间】: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