我是Android新手,单击注销按钮时应用程序崩溃。我不明白为什么。
我释放了mp播放器资源,但仍然崩溃。
用于注销按钮
if(view==logoutbtn)
{
if (mp.isPlaying()) { mp.stop();mp.release();}
firebaseAuth.signOut();
finish();
Intent intent=new Intent(ProfileActivity.this,LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
当前活动上运行的功能是
request.setInterval(5000);
client.requestLocationUpdates(request, new LocationCallback() {
@SuppressLint("NewApi") @Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
if (location != null) {
play_ringtone();
play_flashlight();
send_location_message(location.getLatitude(), location.getLongitude());
}
}
}, null);
我进行活动时的Logcat是:
W/MessageQueue: Handler (android.telephony.PhoneStateListener$1) {ba9c4a5} sending message to a Handler on a dead thread
java.lang.IllegalStateException: Handler (android.telephony.PhoneStateListener$1) {ba9c4a5} sending message to a Handler on a dead thread
短信功能是
private void send_location_message(Double latitude,Double longitude)
{
String msg = "Emergency!!\n\nLocation\n\n"+"https://www.google.com/maps/search/?api=1&query="+latitude+","+longitude ;
for (int i = 0; i < emergency_phones.size(); i++)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(emergency_phones.get(i), null, msg, null, null);
Toast.makeText(ProfileActivity.this, "Message Sent To "+emergency_phones.get(i)+" "+latitude+" "+longitude, Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
将mp.isPlaying()包含在Try catch中,以避免出现空指针异常...
也停止在onDestroy
释放或取消注册 OnDestroy中的位置接收器。 requestLocationUpdates,因为即使您的活动已停止,广播接收器仍将在那里,并且使用新的位置更新,它将在内部运行代码,因此始终必须停止这些线程和BroadCastReceivers