view.findViewById(....) always return null in dialog
Try this..
Change View name
final View main_view = inflater.inflate(R.layout.record_fragment, null);
builder.setView(main_view).setPositiveButton("Save experiment", null);
and use it
EditText x = (EditText) main_view.findViewById(R.id.name);
because in your public void onClick(View view) {
onclick has view
that edit text need to refer inflated
view. Not onclick view.
Or
As like @Raghunandan said change onclick view
name as public void onClick(View v) {
How to resolve findViewByID() returning Null after setContentView()
Please use
addSiteButton =(Button) addSiteSheet.findViewById(R.id.add_site_sheet_add_site_button);
findViewById() returns null for Views in a Dialog
Calling findViewById()
will search for views within your Activity's layout and not your dialog's view. You need to call findViewById()
on the specific View
that you set as your dialog's layout.
Try this
private void initSearch() {
AlertDialog.Builder searchDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
searchDialog.setTitle("Search Photos");
searchDialog.setMessage("Specify tag and value...");
// R.layout.search_dialog is my custom layour, it displays fine, it works.
View dialogView = inflater.inflate(R.layout.search_dialog, null);
searchDialog.setView(dialogView);
EditText tagText = (EdiText) dialogView.findViewById(R.id.tagField);
searchDialog.setPositiveButton( ... ) ...
AlertDialog myAlert = searchDialog.create(); //returns an AlertDialog from a Builder.
myAlert.show();
}
Notice how I'm inflating the view and storing it in a View
named dialogView
. Then, to find your EditText
named tagField
, I'm using dialogView.findViewById(R.id.tagField);
findViewById throws null exception when it is used on Alert Dialog widgets
Replace this:
tv7 = findViewById(R.id.tv7)
bt4 = findViewById(R.id.bt4)
With this:
tv7 = messageBoxView.findViewById(R.id.tv7)
bt4 = messageBoxView.findViewById(R.id.bt4)
You are inflating a view (the dialog) into your activity with:
val messageBoxView = LayoutInflater.from(this).inflate(R.layout.layout_dialog, null)
You then need to define tv7 and bt4 views, but they are in the view that you inflated previously. So you need to define them based on the inflated view.
findViewById Return null inside BottomSheetDialogFragment
Since that button is inflated during onCreateView
, you can set the listener during onCreateView()
:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bottom_filter_dialog, container, false);
// get the views and attach the listener
TextView closeBtn = (TextView) view.findViewById(R.id.taste_filter_close_button);
closeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return view;
}
Note that I'm doing view.findViewById()
and not getActivity().findViewById()
Why findViewById() is returning null
you need to bind views from dialog, so manage it like this;
brushDialog.setContentView(R.layout.brush_size_dialog)
val dialogView = layoutInflater.inflate(R.layout.brush_size_dialog, null)
brushDialog.setContentView(dialogView)
tvBrushSize = dialogView.findViewById(R.id.tv_brush_size)
Related Topics
Date Object Simpledateformat Not Parsing Timestamp String Correctly in Java (Android) Environment
Noclassdeffounderror in Eclipse
How to Write Ndef Records to Nfc Tag
Android Changing Floating Action Button Color
Failed to Run Sdkmanager --List with Java 9
How Can One Detect Airplane Mode on Android
How to Escape % in String.Format
Android "Gps Requires Access_Fine_Location" Error, Even Though My Manifest File Contains This
Android: How to Change the Color of the Datepicker Divider
Variable Is Accessed Within Inner Class. Needs to Be Declared Final
How to Convert Milliseconds to Date Format in Android
How to Change a Textview's Style at Runtime