I want to find the id of a view created programmatically from a runnable set in another class and passed to the searched view's class.
The idea is to lock an application as long as the right password isn't inputed. So when on the main menu the user press "Unlock" button which prompt an overlay dialog from ViewDialog class. showUnlockDialog() from ViewDialog is the function creating and adding a password EditText to a basic dialog set in R.layout.custom_dialog. The user enters the password and click the button which run the runnable fetching the EditText content to retrieve the password entered.
public class MainMenu extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
....
Runnable runnable;
View v;
runnable = new Runnable(){
public void run(){
Runnable runnable = new Runnable(){
public void run(){
EditText mdp = findViewById((int)1);
Log.d("test", mdp.getText().toString());
}
};
ViewDialog alert = new ViewDialog();
alert.showUnlockDialog(MainMenu.this, runnable);
}
};
v = findViewById(R.id.button);
v.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
runnable.run();
}
});
....
}
}
public class ViewDialog extends AppCompatActivity{
Button positiveButton;
Button negativeButton;
....
public void showUnlockDialog(Activity activity, Runnable callback){
final Dialog dialog = new Dialog(activity);
dialog.setCancelable(true);
dialog.setContentView(R.layout.custom_dialog);
....
ConstraintLayout constraintLayout = new ConstraintLayout(activity);
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
constraintLayout.setLayoutParams(params);
EditText password = new EditText(activity);
password.setId((int)1);
constraintLayout.addView(password);
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.customDialogLinLayout);
layout.addView(constraintLayout);
setDialog1Button(activity, dialog, callback);
dialog.show();
}
public void setDialog1Button(Activity activity, final Dialog dialog, final Runnable callback){
positiveButton = new Button(activity);
positiveButton.setText("OK");
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(callback != null) {
callback.run();
}
dialog.dismiss();
}
});
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.customDialogLinLayout);
layout.addView(positiveButton);
}
....
}
When clicking the validation button, password is null in
EditText password = findViewById((int)1);
Log.d("test", password.getText().toString());
and the following error is prompted :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.megacom.inventaire, PID: 18759
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.megacom.inventaire.MainMenu$1$1.run(MainMenu.java:55)
at com.megacom.inventaire.ViewDialog$1.onClick(ViewDialog.java:107)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)