Guys i have a Custom alert dialogue and inside that i am inflating a listView
i want to access the child of that listview but it produces error can anyone help here is my relevant code snippet
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Context);
View alertView = AddExpenses.this.getLayoutInflater().inflate(R.layout.layout_alert_expenses, null);
int amount = Integer.parseInt(etExpensesAmount.getText().toString());
final ListView lvAlertDialogue = (ListView) alertView.findViewById(R.id.lv_alert_expenses);
final TextView tvTotal = (TextView) alertView.findViewById(R.id.tv_amount);
tvTotal.setText(etExpensesAmount.getText().toString());
CustomAdapterAlertList customAdapterAlertList = new CustomAdapterAlertList(AddExpenses.this, listSelectedContact, amount);
lvAlertDialogue.setAdapter(customAdapterAlertList);
alertDialogBuilder.setView(alertView);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
final EditText etAmount;
View singleView = lvAlertDialogue.getChildAt(listSelectedContact.size()-1); //singleView is null here
getChildAt() works for the visible item in list view otherwise it will return null.
Seeing your code I suppose you want the last item. You can try the following code.
View singleView = lvAlertDialog.getChildAt(lvAlertDialog.getLastVisiblePosition()):
In your code you are calling lvAlertDialog.getChildAt(lvAlertDialog.getLastVisiblePosition())
just after the alertDialog.show();
getChildAt is excuteing before showing alertDialog and
getChildAt will work if and only if the dialog is visible.
so it is null.
Try this code. Hope It will work.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Context);
View alertView = AddExpenses.this.getLayoutInflater().inflate(R.layout.layout_alert_expenses, null);
int amount = Integer.parseInt(etExpensesAmount.getText().toString());
final ListView lvAlertDialogue = (ListView) alertView.findViewById(R.id.lv_alert_expenses);
final TextView tvTotal = (TextView) alertView.findViewById(R.id.tv_amount);
tvTotal.setText(etExpensesAmount.getText().toString());
CustomAdapterAlertList customAdapterAlertList = new CustomAdapterAlertList(AddExpenses.this, listSelectedContact, amount);
lvAlertDialogue.setAdapter(customAdapterAlertList);
alertDialogBuilder.setView(alertView);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
View singleView = lvAlertDialogue.getChildAt(listSelectedContact.size() - 1); //singleView is null here
}
});
alertDialog.show();
Related
This question already has answers here:
findviewbyid returns null in a dialog
(7 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
Here is my code:
public void openDialog(){
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
AlertDialog alert = alertDialogBuilder.create();
final EditText a = (EditText) alert.findViewById(R.id.kekekeke);
LayoutInflater inflater = getLayoutInflater();
alertDialogBuilder.setView(inflater.inflate(R.layout.list_example, null));
alertDialogBuilder
.setMessage("Enter a New Name")
.setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());
}
});
}
My problem is that I got below error :
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
I'm confused because I thought that I already accessed it through the edittext.
You can reference it like below
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.list_example, null);
EditText a = (EditText) v.findViewById(R.id.kekekeke);
alertDialogBuilder.setView(v);
Try this,
public void openDialog(){
final AlertDialog.Builder alertDialogBuilder =
new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.list_example, null);
alertDialogBuilder.setView(view);
final EditText a = (EditText) view.findViewById(R.id.kekekeke);
alertDialogBuilder .setMessage("Enter a New Name")
.setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());
}
})
AlertDialog alert = alertDialogBuilder.create();
alert.show();
You should inflate the view first
Problem is your alert dialog has not set the view when you get the reference from it, so it does not have anything until you call setview, so you are calling it before and it is providing you nothing so you get the null pointer
public void openDialog(){
final AlertDialog.Builder alertDialogBuilder =
new AlertDialog.Builder(this);
AlertDialog alert = alertDialogBuilder.create();
LayoutInflater inflater = getLayoutInflater();
alertDialogBuilder.setView(inflater.inflate(R.layout.list_example, null));
final EditText a = (EditText) alert.findViewById(R.id.kekekeke);
alertDialogBuilder .setMessage("Enter a New Name")
.setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());
}
})
Problem is your alert dialog has not to the view when you try to get reference from view, so it does not have anything until you set your view to AlertDialog, So it returns NullPointerException,
Just replace this code with your code
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
AlertDialog alert = alertDialogBuilder.create();
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.list_example, null);
final EditText a = (EditText) view.findViewById(R.id.kekekeke);
alertDialogBuilder.setView(view);
alertDialogBuilder
.setMessage("Enter a New Name")
.setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());
}
});
I am trying to create an alert dialog with an EditText object. I need to set the initial text of the EditText programmatically. Here's what I have.
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
AlertDialog alertDialog = dialogBuilder.create();
LayoutInflater inflater = this.getLayoutInflater();
alertDialog.setContentView(inflater.inflate(R.layout.alert_label_editor, null));
EditText editText = (EditText) findViewById(R.id.label_field);
editText.setText("test label");
alertDialog.show();
What do I need to change so that I can have a valid EditText object?
[edit]
So, it was pointed out by user370305 and others that I should be using alertDialog.findViewById(R.id.label_field);
Unfortunately there is another issue here. Apparently, setting the content view on the AlertDialog causes the program to crash at runtime. You have to set it on the builder.
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));
AlertDialog alertDialog = dialogBuilder.create();
LayoutInflater inflater = this.getLayoutInflater();
EditText editText = (EditText) alertDialog.findViewById(R.id.label_field);
editText.setText("test label");
alertDialog.show();
Unfortunately, when you do this, alertDialog.findViewById(R.id.label_field); now returns null.
[/edit]
editText is a part of alertDialog layout so Just access editText with reference of alertDialog
EditText editText = (EditText) alertDialog.findViewById(R.id.label_field);
Update:
Because in code line dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));
inflater is Null.
update your code like below, and try to understand the each code line
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
dialogBuilder.setView(dialogView);
EditText editText = (EditText) dialogView.findViewById(R.id.label_field);
editText.setText("test label");
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
Update 2:
As you are using View object created by Inflater to update UI components else you can directly use setView(int layourResId) method of AlertDialog.Builder class, which is available from API 21 and onwards.
Use this one
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
// Get the layout inflater
LayoutInflater inflater = (activity).getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the
// dialog layout
builder.setTitle(title);
builder.setCancelable(false);
builder.setIcon(R.drawable.galleryalart);
builder.setView(inflater.inflate(R.layout.dialogue, null))
// Add action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
}
});
builder.create();
builder.show();
You can write:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
LayoutInflater inflater = this.getLayoutInflater();
View dialogView= inflater.inflate(R.layout.alert_label_editor, null);
dialogBuilder.setView(dialogView);
Button button = (Button)dialogView.findViewById(R.id.btnName);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Commond here......
}
});
EditText editText = (EditText)
dialogView.findViewById(R.id.label_field);
editText.setText("test label");
dialogBuilder.create().show();
In case any one wants it in Kotlin :
val dialogBuilder = AlertDialog.Builder(this)
// ...Irrelevant code for customizing the buttons and title
val dialogView = layoutInflater.inflate(R.layout.alert_label_editor, null)
dialogBuilder.setView(dialogView)
val editText = dialogView.findViewById(R.id.label_field)
editText.setText("test label")
val alertDialog = dialogBuilder.create()
alertDialog.show()
Reposted #user370305's answer.
Change this:
EditText editText = (EditText) findViewById(R.id.label_field);
to this:
EditText editText = (EditText) v.findViewById(R.id.label_field);
View v=inflater.inflate(R.layout.alert_label_editor, null);
alertDialog.setContentView(v);
EditText editText = (EditText)v.findViewById(R.id.label_field);
editText.setText("test label");
alertDialog.show();
/**
* Shows confirmation dialog about signing in.
*/
private void startAuthDialog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(800, 1400);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.auth_dialog, null);
alertDialog.getWindow().setContentView(dialogView);
EditText editText = (EditText) dialogView.findViewById(R.id.label_field);
editText.setText("test label");
}
Try making View final
eg:(with custom layout)
final View layout=getLayoutInflater().inflate(R.layout.create_class,null);
builder.setView(layout);
I have the following AlertDialog and its onClick method:
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setView(View.inflate(this, R.layout.barcode_alert, null));
alertDialog.setPositiveButton("Search", new DialogInterface.OnClickListener() {
#Override
public void onClick(#NotNull DialogInterface dialog, int which) {
// how to get editTextField.getText().toString(); ??
dialog.dismiss();
}
});
The XML I inflate in the dialog (barcode_alert.xml) contains, among other things, an EditText field, and I need to get its string value after the user taps the Search button.
My question is, how do I get a reference to that field so I can get its text string?
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
final View v = View.inflate(this, R.layout.barcode_alert, null); //here
alertDialog.setView(v);
alertDialog.setPositiveButton("Search", new DialogInterface.OnClickListener() {
#Override
public void onClick(#NotNull DialogInterface dialog, int which) {
((EditText)v.findViewById(R.id.edit_text)).getText().toString(); //and here
dialog.dismiss();
}
});
Try this,
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
final View v = getLayoutInflater().inflate(this, R.layout.barcode_alert, null);
final EditText editTextField = (EditText) v.findViewById(R.id.edit_text);
editTextField.setOnClickListener(MyActivity.this);
alertDialog.setView(v);
alertDialog.setPositiveButton("Search", new DialogInterface.OnClickListener() {
#Override
public void onClick(#NotNull DialogInterface dialog, int which) {
String enteredValue = editTextField.getText().toString(); // get the text from edit text
dialog.dismiss();
}
});
You can inflate your view and get a reference to a variable:
View v = View.inflate(this, R.layout.barcode_alert, null);
you can use findViewById to get a reference to your view. Make sure that your variable is final or a member variable. Otherwise you cannot access it in the onClickListener
final EditText editText = (EditText)v.findViewById(R.id.your_edit_text
I can create an Android AlertDialog as like this.
AlertDialog.Builder MyDialog = new AlertDialog.Builder(this);
But what matters to me is how can I set a layout for this. When I use a Dialog I can initialize and set a layout as like this.
dialog = new Dialog(Login.this,android.R.style.Theme_Holo_Dialog);
dialog.setContentView(R.layout.journey_details);
can I do something like that to set a layout. Or is in AlertDialog such a functionality not there.
I first created a custom dialog and when I'm setting the time for a text view it gave a null pointer. That's why I tried a AlertDialog. This is my first code. I was unable to figure out the error.
public void ShowJourneyDetails() {
dialog = new Dialog(Login.this,android.R.style.Theme_Holo_Dialog);
crnt_time=(TextView)findViewById(R.id.jd_txt_str_tim);
date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String crnt_date =date.format(new Date()).toString();
crnt_time.setText(crnt_date);
}
This gave me an error. Thats why I tried a AlertDialog.
But my AlterDialog also gives me a NullPointer and the error comes from the button. My code is below
public void ShowJourneyDetails() {
// dialog = new Dialog(Login.this,android.R.style.Theme_Holo_Dialog);
final AlertDialog.Builder MyDialog = new AlertDialog.Builder(this);
LayoutInflater factory = LayoutInflater.from(Login.this);
final View DialogView = factory.inflate(R.layout.journey_details, null);
MyDialog.setView(DialogView);
LoginIntent = new Intent(Login.this, DropPickUp_List.class);
check_fuel = (CheckBox) findViewById(R.id.jd_chk_ful);
check_e_oil = (CheckBox) findViewById(R.id.jd_chk_eoil);
check_wtr = (CheckBox) findViewById(R.id.jd_chk_wtr);
check_b_oil = (CheckBox) findViewById(R.id.jd_chk_boil);
check_lic = (CheckBox) findViewById(R.id.jd_chk_lc_inc);
check_gt_pass = (CheckBox) findViewById(R.id.jd_chk_gt_ps);
check_bills = (CheckBox) findViewById(R.id.jd_chk_bil);
crnt_time = (TextView) findViewById(R.id.jd_txt_str_tim);
dialogButton = (Button) dialog.findViewById(R.id.jd_btn_strt);
MyDialog.setView(check_fuel);
MyDialog.setView(check_e_oil);
MyDialog.setView(check_wtr);
MyDialog.setView(check_b_oil);
MyDialog.setView(check_lic);
MyDialog.setView(check_gt_pass);
MyDialog.setView(check_bills);
MyDialog.setView(dialogButton);
MyDialog.setView(crnt_time);
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String crnt_date = date.format(new Date()).toString();
crnt_time.setText(crnt_date);
final Stack<String> ItemStack=new Stack();
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(Login.this, msg, Toast.LENGTH_LONG).show();
}
});
MyDialog.show();
}
I can't figure out the error.
If you want to create Custom AlertDialog then you need to Inflate Layout Like below:
LayoutInflater factory = LayoutInflater.from(this);
final View dialogView = factory.inflate(
R.layout.custom_progress_layout, null);
and set this Layout to your Dialog like below:
Dialog main_dialog = new Dialog(this, R.style.Theme_Dialog);
main_dialog.setContentView(dialogView);
That's it.
Yes. You can do it this way:
LayoutInflater mLayoutInflator = LayoutInflater.from(this);
final View customDialogView = mLayoutInflator.inflate(
R.layout.journey_details, null);
final AlertDialog customDialog= new AlertDialog.Builder(this).create();
customDialog.setView(customDialogView);
customDialog.show();
You can call
http://developer.android.com/reference/android/app/AlertDialog.html#setView(android.view.View)
on the AlertDialog.
I'm trying to have my AlertDialog with a custom list view but can't seem to get it to show or run without error.
private void buildDialog(){
int selectedItem = -1; //somehow get your previously selected choice
LayoutInflater inflater = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
View customView = inflater.inflate(R.layout.listview, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView);
builder.setTitle("Select Weapon").setCancelable(true);
builder.setSingleChoiceItems(inventory, selectedItem, "Desc", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
ListView lv = ((AlertDialog) dialog).getListView();
itemId = lv.getAdapter().getItemId(which);
new changeEQ().execute();
}
});
dialog = builder.create();
}
This is my AlertDialog but can't figure out what to add to get my custom layouts, listview & listrow to be used. I've looked around at guides online but nothing they show seems to work for me. IE I must be doing something wrong.
EDIT: changed code to include answer but has no change on what is showed on screen. No errors yet no change in look.
If you have a custom layout that you want to pass to your AlertDialog try:
LayoutInflater inflater = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
View customView = inflater.inflate(R.layout.custom_dialog, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView);
If you want to define listeners try:
ListView list = (ListView) customView.findViewById(R.id.listView1);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Do as you please
}
});