'Attempted to finish an input event but the input event receiver has already been disposed error
I am not sure what I have done but for a moment my code was working smoothly and after I added a new activity the error Attempted to finish an input event but the input event receiver has already been disposed.
I need help on how to fix this.
package proj.com.desperationfinals;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
}
public void validate (View view){
String word = editText.getText().toString();
if(word.contentEquals("Sir Zalameda")) {
AlertDialog.Builder Alert = new AlertDialog.Builder(this);
Alert.setMessage("Correct!")
.create();
Alert.show();
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
}else{
AlertDialog.Builder Alert = new AlertDialog.Builder(this);
Alert.setMessage("Mali!")
.create();
Alert.show();
}
}
}
Solution 1:[1]
The problem is in the following section:
AlertDialog.Builder Alert = new AlertDialog.Builder(this);
Alert.setMessage("Correct!")
.create();
Alert.show();
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
You are getting the error because you are trying to show an alert right before starting a new activity. Now, the 'receiver' for the dialog is the current activity and as soon as you start new activity that is switched. To tackle this problem you can try something like this:
Alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
}
TL;DR: Can't show an alert right before starting a new activity.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Shaishav |