.dismiss() is showing an error in AlertDialog - java

I have the following code:
AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
View view = LayoutInflater.from(getActivity()).inflate(R.layout.displayfilecontents, null);
EditText text = (EditText) view.findViewById(R.id.etFileContents);
if (text != null) {
text.setFocusable(false);
text.setLongClickable(false);
text.setTextIsSelectable(false);
}
text.setText(builder);
b.setView(view);
b.setTitle("Trip Name: " + FilesInFolder.get(position).toString().substring(0, FilesInFolder.get(position).toString().lastIndexOf(".")));
Button btnCloseIt = (Button) view.findViewById(R.id.btnClose);
btnCloseIt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
b.dismiss();
}
});
AlertDialog dl = b.create();
dl.show();
I am trying to dismiss the dialog once the btnCloseIt is pressed. I am receiving an error on this line:
b.dismiss(); //giving an error
Error: The method dismiss() is undefined for the type AlertDialog.Builder
Update: [RESOLVED]
// custom dialog
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.displayfilecontents);
dialog.setTitle("Trip Name: " + FilesInFolder.get(position).toString().substring(0, FilesInFolder.get(position).toString().lastIndexOf(".")));
EditText text = (EditText) dialog.findViewById(R.id.etFileContents);
if (text != null) {
text.setFocusable(false);
text.setLongClickable(false);
text.setTextIsSelectable(false);
}
text.setText(builder);
Button btnCloseIt = (Button) dialog.findViewById(R.id.btnClose);
// if button is clicked, close the custom dialog
btnCloseIt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();

As others have already pointed out, b is a reference to AlertDialog.Builder and not to the Dialog itself. AlertDialog.Builder class doesn't have any method named dismiss(). Save a reference to the Dialog which is returned to you when you call create() or show() method from AlertDialog.Builder class.
One more thing, since you are calling create() and show() methods at the same time, do you really want to call both the methods? I believe calling only show() method would suffice for you here. From Developer Reference public AlertDialog show () : Creates a AlertDialog with the arguments supplied to this builder and show()'s the dialog.

You need to store the result of calling b.create(); that's what you need to call dismiss() on.

Related

Cancel/dismiss alertdialog builder from any other method in same class in android?

I have a java class not activity. From any other activity, I called this class and in that class I created alertdialog builder. In that I inflate data from db.
Now In this class, I have other listener and method also. In one of the method, I want to dismiss/cancel this dialog. Like how we do
setResult(RESULT_OK, intent);
finish();
in any activity, same thing I want to do in class.
Code: This method I called from activity.
public void showProvidersDialog(long customCategoryId) {
categoryId = customCategoryId;
LayoutInflater li = LayoutInflater.from(context);
promptsView = li.inflate(R.layout.row_providers_layout, null);
init();
alertDialogBuilder = new android.app.AlertDialog.Builder(context, R.style.dialogBoxStyle);
alertDialogBuilder.setView(promptsView);
alertDialogBuilder.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
isInsurance();
alertDialogBuilder.show();
//solved: dialog = alertDialogBuilder.create();
dialog.show();
}
And I have one more method in same java class, from that method I want to dismiss currentlt opened dialog.
private void sendProviderData(General provider) {
Singleton.getInstance().setProviderId(provider.getId());
Singleton.getInstance().setProviderIcon(provider.getIcon());
Singleton.getInstance().setProviderName(provider.getName());
//solved
dialog.dismiss
}
Explain once again: See, I can cancel dialog inside negative button. But what I want is, in that dialog I inflate row which includes one contact list. I want that when user click on any of the contact (which is let's say clicked on recycler on touch listener) I'm passing some data using singleton and at that same time, I want to dismiss dialog.
Here's dialog code for generic purpose. You can call whenever you need to show the dialog. You can show dialog by calling showDialog() method & dismiss by calling dismissDialog() method.
/*
* call whenever dialog is required in whole app in form of popup
*/
public class MyDialog implements View.OnClickListener {
private Dialog dialog;
private Context context;
private TextView tvTitle;
private TextView tvSubtitle;
private Button bt_ok;
private String strInvalidUserNamePass, strHeader;
/*
* constructor to change the text dynamically.
*/
public MyDialog(Context context, String strHeader, String invalidUserNamePass) {
this.context = context;
this.strInvalidUserNamePass = invalidUserNamePass;
this.strHeader = strHeader;
if (context != null) {
initDialog();
}
}
/*
* set id of all the view components and implement listeners
*/
private void initDialog() {
dialog = new Dialog(context, R.style.FMDialogNormal);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_validation);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
dialog.show();
tvTitle = (TextView) dialog.findViewById(R.id.tv_title);
tvSubtitle = (TextView) dialog.findViewById(R.id.tv_subtitle);
tvTitle.setText(strHeader);
tvSubtitle.setText(strInvalidUserNamePass);
bt_ok = (Button) dialog.findViewById(R.id.bt_ok);
bt_ok.setOnClickListener(this);
}
/*
* Implement listener according to the views
*/
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_ok:
dialog.dismiss();
break;
}
}
public void showDialog(){
if(dialog!=null){
dialog.show();
}
}
public void dismissDialog(){
if(dialog!=null && isVisible()){
dialog.show();
}
}
public boolean isVisible() {
if (dialog != null) {
return dialog.isShowing();
}
return false;
}
}

Capturing user input from an actionbar menu item

I am creating a gym app in Android Studio and the first feature I'm trying to implement is to have the user create a workout by clicking on an option in the action bar to add it. Clicking this button brings up an alert dialog with an EditText field to type in the workout name. Later, I will use the input to create a list view with the different workouts added, but for now I am just concerned about capturing the input from this EditText field.
Here is what should happen.. on this screen I click the + button and it brings up an alert dialog box with an EditText field. I want to capture this input in the java main activity file.
Here is the java MainActivity File. I want the input from the EditText field to be stored in the m_Text variable.
public class MainActivity extends AppCompatActivity {
private String m_Text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
//Clicking add workout button in the action bar
//stackoverflow.com/questions/13143006/alert-dialog-from-within-onooptionsitemselected-android
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_add_workout:
//final EditText mAddWorkout = (EditText)R.layout.userinput;
//Creating the dialog box for entering the workout name
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter the workout name");
//Create the user input xml file into a java object; capturing the user input from the dialog box
//inflate means "fill"
View view = LayoutInflater.from(this).inflate(R.layout.userinput,null);
final EditText mAddWorkout = (EditText)view.findViewById(R.id.workout_name_input);
builder.setView(R.layout.userinput);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
m_Text = mAddWorkout.getText().toString();
boolean brkpt = true;
}
}); //Second parameter pass in which event listener should trigger when the button is clicked
builder.setNegativeButton("Cancel",null);
builder.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here is the xml for my actionbar menu item for adding the workout name (main_menu.xml)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_add_workout"
android:icon="#drawable/ic_add"
android:title="#string/add_workout"
app:showAsAction="always"/>
</menu>
Last, the xml for the EditText (userinput.xml)
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/workout_name_input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="name..">
</EditText>
When I debug my code, the m_Text variable is always empty if I enter a workout name in the EditText field. I have been stuck on this for days now and I have combed youtube and SO for an answer and haven't found much relating to my issue.
Any insight is greatly appreciated. Thank you.
EDIT: Updated code for MainActivity. I can get control pass to the custom clicker but the input is still not saved. Thanks
public class MainActivity extends AppCompatActivity {
private EditText mAddWorkout;
public class CustomClickListener implements View.OnClickListener {
private final Dialog dialog;
CustomClickListener(Dialog dialog) {
this.dialog = dialog;
}
#Override
public void onClick(View v) {
String editTextValue= mAddWorkout.getText().toString();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
//Clicking add workout button in the action bar
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_add_workout:
//final EditText mAddWorkout = (EditText)R.layout.userinput;
//Creating the dialog box for entering the workout name
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter the workout name");
//Create the user input xml file into a java object; capturing the user input from the dialog box
//inflate means "fill"
View view = LayoutInflater.from(this).inflate(R.layout.userinput,null);
mAddWorkout = (EditText)view.findViewById(R.id.workout_name_input);
builder.setView(R.layout.userinput);
builder.setPositiveButton("OK",null);
builder.setNegativeButton("Cancel",null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
Button saveWorkout = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
saveWorkout.setOnClickListener(new CustomClickListener(alertDialog));
builder.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
******FINAL EDIT*******
I've since finished the entire app and thought I would post my alert dialog code in case it helps someone else out. It turns out that this issue with alert dialogs was the only major issue I had, once I got used to android studio and java things really took off. Anyways my gymapp is a nice little app that uses SQLlite to track workouts, exercises, and sets.. I've actually used it in the gym :)
#Override
//Clicking add workout button in the action bar
public boolean onOptionsItemSelected(MenuItem item) {
//Creating the dialog box for entering the workout name
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
builder.setTitle("Enter the workout name").setView(input).setView(input);
builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Workout workout = new Workout(input.getText().toString());
long workout_key = myDb.createWorkout(workout);
populateWorkouts();
}
});
builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
return super.onOptionsItemSelected(item);
}
In my case, I use the input to create a workout object and insert it to my database. And obviously you would need a switch statement if you had more than one option in your action bar menu.
Thanks again to the 2 guys that tried to help me.
Here is how you should do this:
Create a customer click listener
private class CustomClickListener implements View.OnClickListener {
private final Dialog dialog;
CustomClickListener(Dialog dialog) {
this.dialog = dialog;
}
#Override
public void onClick(View v) {
String editTextValue= mAddWorkout.getText().toString();
}
}
You should make mAddWorkout value class-level so you can access it easily!
Then :
Set this click listener to your dialog like this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter the workout name");
//Create the user input xml file into a java object; capturing the user input from the dialog box
//inflate means "fill"
View view = LayoutInflater.from(this).inflate(R.layout.userinput,null);
final EditText mAddWorkout = (EditText)view.findViewById(R.id.workout_name_input);
builder.setView(R.layout.userinput);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
boolean brkpt = true;
}
}); //Second parameter pass in which event listener should trigger when the button is clicked
AlertDialog alertDialog = builder.create();
alertDialog .show();
Button saveWorkout = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
saveWorkout .setOnClickListener(new CustomClickListener(alertDialog));
This is the solution that I use for my own code and works fine; you can do validation inside the onClick method of the click listener and alert the user accordingly!
Good luck!
private android.app.AlertDialog mAlert;
private EditText mAddWorkout;
//Creating the dialog box for entering the workout name
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = LayoutInflater.from(this).inflate(R.layout.userinput,null);
builder.setTitle("Enter the workout name");
mAlert = builder.create();
mAlert.setCancelable(true);
mAlert.setView(view, 10, 10, 10, 10);
if (mAlert != null && !mAlert.isShowing()) {
mAlert.show();
}
mAddWorkout = (EditText) mAlert.findViewById(R.id.workout_name_input);
m_Text = mAddWorkout.getText().toString();
Hope this solves your problem
A bit late, but I had run into the same problem, and found yet another answer. The solution that works for me is...
binding.myEditText.setOnEditorActionListener { _, action, key ->
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
key == null ||
key.keyCode == KeyEvent.KEYCODE_ENTER) {
// Put your actions here
true
} else {
false
}
}
I have only been writing kotlin for a month or so, so I cannot promise this follows best practice, but maybe someone else can improve this.
This task is hard because we had only one control, so it is harder to trap focus changes. If we had a page full of buttons, we might have a [Reset] and a [Save] button. We have one function that loads all the parameters into the EditTexts (used on entry and Reset); and one function that updates all the parameters with the EditText text (used on Save). Then all is easy. But I was going to get one parameter going before adding th fancy stuff, which is why I fell into this hole.

Nested AlertDialog

I'm facing a unreasolvable (for me) problem with nested AlertDialog using the following code
final AlertDialog.Builder button_cook_action = new AlertDialog.Builder(this);
final EditText cookMl = new EditText(this);
cookMl.setInputType(InputType.TYPE_CLASS_NUMBER);
button_cook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button_cook_action.setTitle(R.string.kitchen_recipe_button_cook)
.setMessage(R.string.kitchen_recipe_button_cook_volume)
.setView(cookMl)
.setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
AlertDialog.Builder builderCooking = new AlertDialog.Builder(RecipeActivity.this);
builderCooking.setTitle(recipe.getName())
.setMessage("message");
builderCooking.show();
}
})
.setNegativeButton(R.string.No, null)
.show();
}
});
The first call works fine, but when i call it for a second time it gave me :
FATAL EXCEPTION: main
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I already search in this forum but without any success.
If someone has a clue. Thanks in advance :)
You can do it like this - the problem was before if you use the EditText a second time it already has a parent - you need to create a new one each time inside your onClick() :
button_cook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final AlertDialog.Builder button_cook_action = new AlertDialog.Builder(this);
final EditText cookMl = new EditText(this);
cookMl.setInputType(InputType.TYPE_CLASS_NUMBER);
button_cook_action.setTitle(R.string.kitchen_recipe_button_cook)
.setMessage(R.string.kitchen_recipe_button_cook_volume)
.setView(cookMl)
.setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
AlertDialog.Builder builderCooking = new AlertDialog.Builder(RecipeActivity.this);
builderCooking.setTitle(recipe.getName())
.setMessage("message");
builderCooking.show();
}
})
.setNegativeButton(R.string.No, null)
.show();
}
});
The problem is in the setView of your alertDialog. You have to inflate the layout everytime your create your dialog. In your case, your are inflating an EditText. So either you should create your EditText inside button_cook onClickListener or adopt the solution as posted by #ligi.

Alertdialog.Builder's setItems() onClick returning null dialog

I am making a custom Alertdialog.Builder and took the liberty of extending the work done by danoz73, found here https://github.com/danoz73/QustomDialog.
I have implemented my own setItems() method in the class QustomDialogBuilder.java (a subclass of AlertDialog.Builder). Since the items I pass are not the standard, the method is NOT marked as Override.
public QustomDialogBuilder setItems(ArrayList<KMColor> items, final DialogInterface.OnClickListener listener) {
LinearLayout itemList = (LinearLayout) mDialogView.findViewById(R.id.items_list);//INFLATE THE LINEARLAYOUT INSIDE THE SCROLLVIEW
Log.i("TAG","1 setItems items dialog is "+mDialog);//<--THIS IS NULL
//HIDE UNUSED VIEWS SO THERE ISN'T A SPACE
mDialogView.findViewById(R.id.customPanel).setVisibility(View.GONE);
mDialogView.findViewById(R.id.contentPanel).setVisibility(View.GONE);
for (int i = 0; i < items.size(); i++) {
KMColor item = items.get(i);
View listItem = inflateItem(item.name,item.color);
listItem.setId(item.color);//VIEWS IDS CAN'T BE 0
itemList.addView(listItem);
Log.i("TAG","2 setItems items dialog is "+mDialog);//<--THIS IS NULL
if (listener != null) {
/*itemList <-- doesn't tell you what item was clicked*/
listItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("TAG","3 setItems items dialog is "+mDialog);//<--STILL NULL
listener.onClick(mDialog, v.getId());
}
});
}
}
return this;
}
This all works fine I get the custom dialog I want to display. BUT when I select an item I get the id however my dialog retuned from the onClick is null so I cannot dismiss the dialog!
alert.setItems(items,new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("TAG", "setItems dialog is " + dialog);//<--null, so I crash if I try to dismiss()
}
});
Back in QustomDialogBuilder.java here is the show override I use to grab the dialog.
#Override
public AlertDialog show() {
if (mTitle.getText().equals("")) mDialogView.findViewById(R.id.topPanel).setVisibility(View.GONE);
if (mMessage.getText().equals("")) mDialogView.findViewById(R.id.contentPanel).setVisibility(View.GONE);
mDialog = super.show();//<--GET THE DIALOG TO PASS IN THE ONCLICK
Log.i("TAG","AlertDialog show() dialog is "+ mDialog);
return mDialog;
}
I'm sure I could grab reference to the dialog in a different manner however I'd really like to understand why this isn't quit working the way I expect it to. I would appreciate any help I can get. Thanks!
EDIT Here is how I build the dialog
public void showColorPicker(){
QustomDialogBuilder alert = new QustomDialogBuilder(context);
alert.setTitle("Pick Color");
alert.setTitleColorWithResource(getResources().getColor(R.color.white));
alert.setDividerColorWithResource(getResources().getColor(R.color.white));
ArrayList<KMColor> items = new ArrayList<KMColor>();
items.add(new KMColor("Black",Color.BLACK));
items.add(new KMColor("Blue",Color.BLUE));
items.add(new KMColor("Cyan",Color.CYAN));
items.add(new KMColor("Green",Color.GREEN));
items.add(new KMColor("Dark Grey",Color.DKGRAY));
items.add(new KMColor("Light Grey",Color.LTGRAY));
items.add(new KMColor("Magenta",Color.MAGENTA));
items.add(new KMColor("Red",Color.RED));
items.add(new KMColor("White",Color.WHITE));
items.add(new KMColor("Yellow",Color.YELLOW));
alert.setItems(items,new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("TAG", "setItems dialog is " + dialog);//<--NULL
}
});
alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();//<--WORKS FINE
}
});
// create alert dialog
AlertDialog alertDialog = alert.create();
// show it
alertDialog.show();
}
Embarrassed to say I found the answer. My problem was here
// create alert dialog
AlertDialog alertDialog = alert.create();
// show it
alertDialog.show();
I was calling create() on my Alertdialog.Builder however I was not Overrideing the create() in QustomDialogBuilder.java, I was only Overrideing the show() therefore I never got a handle on the dialog! simple fix! The above code is now just
alert.show();
I now also Override the create() in QustomDialogBuilder.java. Same thing as the show() but now I won't accidentally chase my tail!
#Override
public AlertDialog create() {
if (mTitle.getText().equals("")) mDialogView.findViewById(R.id.topPanel).setVisibility(View.GONE);
if (mMessage.getText().equals("")) mDialogView.findViewById(R.id.contentPanel).setVisibility(View.GONE);
mDialog = super.create();
return mDialog;
}

findviewbyid returns null in a dialog

I have a custom dialog and when I try to get the value of an EditText it returns null.
This line returns null
EditText et = (EditText)findViewById(R.id.username_edit);
Here is the code in its entirety.
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_TEXT_ENTRY:
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(TicTacToe.this)
//.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(getTitleText())
.setView(textEntryView)
.setPositiveButton("JOIN GAME", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try
{
EditText et = (EditText)findViewById(R.id.username_edit);
playerName = et.getText().toString();
}
catch (Exception e)
{
}
}
})
.create();
}
return null;
}
In my case:
First I must call the
dialog.show(),
and only after it I was able to use
dialog.findviewById(R.id.myID).
If I missed to call the show(), than I got a null back with findViewByID.
Try this:
EditText et = (EditText)textEntryView.findViewById(R.id.username_edit);
You have to tell in which view to find the id. Otherwise it will try to find the id in the view from the xml layout inflated by setContentView (usually declared in onCreate)
I faced a similar problem. In my case, I had a dialog with custom layout and in this layout had a radioButton. In order to solve that, I used the follow code:
View dialogLayout = factory.inflate(R.layout.dialog_layout, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setView(dialogLayout);
RadioButton radiobt = (RadioButton) dialogLayout.findViewById(R.id.radioBt);
I was having the same problem, which presented itself in the following code snippet:
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.addbank_dialog);
dialog.show();
Button btnSaveBank = (Button)dialog.findViewById(R.id.btnSaveBank);
final EditText etBankName = (EditText)dialog.findViewById(R.id.etBankName);
btnSaveBank.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try{
String bank = etBankName.getText().toString();
SharedCommonData.dbOps.saveBankInDB(bank);
}
catch(Exception e){
e.printStackTrace();
}
Toast.makeText(SharedCommonData.context, "Bank Saved", Toast.LENGTH_SHORT);
refreshBanks();
dialog.dismiss();
}
});
etBankName was returning null value, but then I used dialog.findviewbyid(R.id.etBankName) and it worked.
In my case I had this error because I was redeclaring an initialized variable
In main Activity I had:
EditText cityName;
And in onCreate:
EditText cityName = (EditText)findViewById(R.id.cityName);
Just removed EditText and smooth sailing!
None of the existing answers worked for me, so I started trying different lifecycle hooks, and the one that worked for me was onViewCreated, which seems like a good choice semantically as well.
In my case what I did was that I was passing an ID of a view which was in different fragment and in that case the compiler gave me same error.
So try checking that the ID use pass is in the same xml which is connect to the java file.
I hope it helps this is my first ever solution given in this community.

Categories