I’m remotely collaborating with a friend on an Android project and when I pulled his changes to my local repo, I get the following error message for a call made on an AlertDialog object:
DialogInterface.OnShowListener() cannot be resolved to a type
Here’s the full code:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
...
...
protected AlertDialog mDialogForgotPassword;
...
...
mDialogForgotPassword = new AlertDialog.Builder(LogInActivity.this)
.setTitle(getString(R.string.forgot_password))
.setView(input)
.setCancelable(false)
.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// NOTE: LEAVE THIS AS EMPTY
// WE OVERRIDEN THIS METHOD USING THE setOnShowListener
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
})
.create(); // created AlertDialog
// ERROR APPEARS NEXT, red line under "new DialogInterface.OnShowListener()"
mDialogForgotPassword.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
final Button positiveButton = mDialogForgotPassword.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String EMAIL = input.getText().toString();
if( ValidationHelper.isEmail(EMAIL) ){
resetUserPassword(EMAIL);
}
}
});
}
}); // end setOnShowListener
mDialogForgotPassword.show();
The friend says that when he presses Ctrl + Space in Eclipse after typing mDialogForgotPassword, the method setOnShowListener() appears as a suggestion. To me, however, it does not, but the import statements seem to be complete. Help?
What I did to eliminate the error:
Made sure my Java compiler in Eclipse is set to 1.6. Right-clicked on project –> Java Compiler –> Check “Enable project specific settings” –> Set “Compiler compliance level” to 1.6 –> Check “Use default compiler compliance settings.”
Right-clicked on project –> Android –> Checked “Android 4.1” for the project build target. As of this writing, Jelly Bean is the latest release.
In my Android manifest, I upped the minimum SDK to 8 (Froyo, 2.2) and set the targetSdk to 16, Jelly Bean. API level 4 was too low for a minimum anyway, and according to Android distribution data, has a very small and shrinking market share.