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.
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);
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)
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()
(ImageView)findViewById() always returns null
It may have mistaken Activity view from Detailed view. I bet your ImageView is under Detailed View since it was "layout_below:detailedTextView". Try Changing it to
ImageView picture = (ImageView) detailed.findViewById(R.id.detailedImageView);
Related Topics
Android; Geocoder, Why Do I Get "The Service Is Not Available"
Java.Lang.Noclassdeffounderror: Com.Google.Android.Gms.R$Styleable in Android
Toast Created in an Intentservice Never Goes Away
How to Customize Listview Row Android
Setpluginsenabled Not Exist for Webview
Android 11 Action_Open_Document_Tree: Set Initial Uri to the Documents Folder
Add Google Play Services to Eclipse Project
How to Disable Home Button in Android
Noclassdeffounderror at Google Play Services V2 Library
Android Action_Shutdown Broadcast Not Working
Android: Xml Layout for a Listview with Different Items
Android Mapactivity:Couldn't Get Connection Factory Client
How to Make My Own Custom Dialer in an Android Phone
Can't Find Toolbar in an Included Layout
Is My App or Its Dependencies Violating the Android Advertising Id Policy