Cannot set EditText? Issue From dialog in a fragment - Strange - Android Java - java

I've come across an issue which I've never had before. I have a Fragment containing a button. This button (ViewBookingButton) shows a popup dialog. When the dialog opens I would like to set a string in an EditText (AllBooking). Unfortunately it crashes and shows NullPointerExecption. What is going wrong? Here is my code:
ViewBookingButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
AlertDialog.Builder ViewBookingHelpBuilder = new AlertDialog.Builder(getActivity());
ViewBookingHelpBuilder.setTitle("view booking:");
LayoutInflater inflater = getActivity().getLayoutInflater();
View DialogLayout = inflater.inflate(R.layout.view_booking, null);
ViewBookingHelpBuilder.setView(DialogLayout);
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
AllBooking.setText("Hello World");//WHEN I REMOVE THIS THE DIALOG WORKS
ViewBookingHelpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
}
});
AlertDialog helpDialog = ViewBookingHelpBuilder.create();
helpDialog.show();
}
});

Change this
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
to
TextView AllBooking = (TextView)DialogLayout.findViewById(R.id.AllBooking);
TextView belongs to the inflated layout and `findViewById looks for the view in the current view hierarchy.

Related

Alert dialog in Fragment, app get crashed

I want to display the alert dialog when onClick of the button in fragment layout. I have tried but the app is crashing when it runs.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentWalletBinding.inflate(inflater, container, false);
Button button = (Button)getView().findViewById(R.id.add_money);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder alertdialog = new AlertDialog.Builder(view.getContext());
alertdialog.setMessage("this is message");
alertdialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(),"toast message",Toast.LENGTH_SHORT).show();
}
}).show();
}
});
return binding.getRoot();
}
onCreateView returns View which can be further obtained by getView() method. and you are calling it before onCreateView had a chance to return own View for framework (its still inside method, didn't return binding.getRoot(); yet)
if you are using view binding then obtain your Button from there
Button button = (Button) binding.addMoney;
or even just use strictly (without additional local Button)
binding.addMoney.setOnClickListener(...
and if you REALLY WANT to use findViewById (but why...), then you can look inside binding.getRoot() even when it isn't returned/drawn yet
Button button = (Button) binding.getRoot().findViewById(R.id.add_money);
there is a problem in
(Button)getView().findViewById(R.id.add_money);
please make sure id is same as in XML.

must call removeView() on the child's parent first

I'm creating a chat application and facing a problem while creating a group (Recycler View's List item) and to inflate it over the fragment container.
Actually, two kind of views are being inflated. One is the root View that will be inflated in a fragment container and second is inflated as another custom layout for Dialog ask for input group name.
FAB is implemented as a click event to launch Dialog to input channel name. First time when i click a FAB, dialog appears ask for channel name to create group. After passing, an input group is created and Channel/Group (Recycler View's List item) is created and inflated over the container. But second time when I click on FAB, to repeat the process, to create another group/channel, the app crashes and error is encountered on logcat.
"The specified child already has a parent. You must call removeView() on the child's parent first".
I don't have too much knowledge about fragments and don't know what does that error means. Please help to resolve this error.
Thanks
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_open_channel_list, container, false);
CFAlertDialogue_footerView = inflater.inflate(R.layout.cfalertdialog_footer_view_open_channel, null);
editText_ChannelName = CFAlertDialogue_footerView.findViewById(R.id.textInputEditText_channelName);
setRetainInstance(true);
mRecyclerView = rootView.findViewById(R.id.recyclerView_openChannelList);
channelListAdapter = new openChannelListAdapter(getContext());
refreshLayout = rootView.findViewById(R.id.swipe_refresh_open_channel_list);
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
refreshLayout.setRefreshing(true);
refreshChannelList(CHANNEL_LIST_LIMIT);
}
});
actionButton = rootView.findViewById(R.id.fab_addOpenChannel);
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CFAlertDialog.Builder dialog = new CFAlertDialog.Builder(getContext())
.setDialogStyle(CFAlertDialog.CFAlertStyle.ALERT)
.setTitle("Create Open Channel")
.setTextGravity(Gravity.CENTER_HORIZONTAL)
.setCornerRadius(5)
.setCancelable(true)
.setFooterView(CFAlertDialogue_footerView)
.addButton("Cancel", Color.parseColor("#000000"), Color.parseColor("#f8f8ff"),
CFAlertDialog.CFAlertActionStyle.NEGATIVE, CFAlertDialog.CFAlertActionAlignment.JUSTIFIED,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.addButton("Create", Color.parseColor("#000000"), Color.parseColor("#8b3a3a"),
CFAlertDialog.CFAlertActionStyle.POSITIVE, CFAlertDialog.CFAlertActionAlignment.JUSTIFIED,
new DialogInterface.OnClickListener() {
#Override
public void onClick(final DialogInterface dialog, int which) {
OpenChannel.createChannelWithOperatorUserIds(editText_ChannelName.getText().toString(),
null, null, null, null, new OpenChannel.OpenChannelCreateHandler() {
#Override
public void onResult(OpenChannel openChannel, SendBirdException e) {
if ( e != null){
Toast.makeText(getContext(), "Error Creating Channel: "+e.getCode()+" "
+e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
dialog.dismiss();
refreshChannelList(CHANNEL_LIST_LIMIT);
}
});
}
});
dialog.show();
}
});
setUpRecyclerViewAndAdapter();
return rootView;
}
Change to
final AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
b.setView(layout);
b.show();
to
final AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
b.setView(layout);
final AlertDialog alertDialog = b.show();
Try to change something like this

popping up an AlertDialog on click a button in a ListView

I have ListView that is populated using a CursorAdapter. In each ListView row there are too textviews and a button. An AlerDialog is supposed to pop on button click.The AlertDiaog itself is filled with a cursor.
the problem is that when i try to make and alert dialog builder it needs an argument that i don't know how to get it. the code that i"m posting here has a syntax error that i don't know how to fix it.
i tried to move the ItemClickListener to original activity class but the button has null value when i try to assign it to an object.
this is the bindView of the CursorAdapter:
public void bindView(final View view, Context context, Cursor cursor) {
//ListView list= (ListView) view.findViewById(R.id.words);
Button addButton=(Button)view.findViewById(R.id.add_btn);
addButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view1) {
//Dictionary d=new Dictionary();
AlertDialog.Builder builder=new AlertDialog.Builder(Dictionary.this); // the error
final Cursor boxes=Dictionary.getCursor();
builder.setCursor(boxes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Dictionary.getCursor().moveToPosition(i);
String text = boxes.getString(boxes.getColumnIndex("_id"));
Log.d("select", "onClick: "+text);
}
}, "name");
builder.show();
}
});
TextView english=(TextView) view.findViewById(R.id.english);
TextView farsi=(TextView) view.findViewById(R.id.farsi);
english.setText(cursor.getString(1));
farsi.setText(cursor.getString(2));
}

Android popup on activity create

I am using the following code to create a popup which shows as the activity loads.
I call loadingPopup(); in my oncreate method.
The popup works the problem is when ever i try to place onclick listener for the dismiss button the whole thing crashes on load. If i were to remove the DissMissButton onclick listener code it works fine. Any help would be appreciated
private void loadingPopup() {
LayoutInflater inflater = this.getLayoutInflater();
final View layout = inflater.inflate(R.layout.popup_welcome, null);
final Button DismissButton = (Button) findViewById(R.id.button_welcomepopup_dismiss);
final PopupWindow Welcome_popupWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
Welcome_popupWindow.setFocusable(false);
Welcome_popupWindow.setTouchable(true);
Welcome_popupWindow.setOutsideTouchable(true);
layout.post(new Runnable() {
public void run() {
Welcome_popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
DismissButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Welcome_popupWindow.dismiss();
}
});
}
});
You call find view on your activity view, while your button is in the popup, so of course the DismissButton is null and app force close.
Change the find view code to this:
final Button DismissButton = (Button) layout.findViewById(R.id.button_welcomepopup_dismiss);

android keep dialogbox open until user click on button [duplicate]

i am having an custom alert view which pop ups on a button click event.all the things are going fine.but the problem is:
if user clicks outside alert dialog it disappear.i want to restrict user for clicking out side.I am giving him the choice of cancel/cross button to close alert dialog.
so how to restrict user clicking outside the alert box?
code:
the code in onCreate for button click where i am calling show dialog:
final Button cdButton = (Button) findViewById(R.id.denonCdImage);
cdButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
showDialog(CD_CATG_ID);
}
});
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder;
Context mContext = this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.categorydialog,(ViewGroup) findViewById(R.id.layout_root));
GridView gridview = (GridView)layout.findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
/** Check the id for the device type for image tobe change */
switch(id) {
case 1 : // for the cd image
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {
Toast.makeText(view.getContext(), "Image selected for CD", 3000).show();
cdImageId = getImageId(position);
int elementId = getApplicationContext().getResources().getIdentifier(cdImageId, "drawable", getPackageName());
cdImageView.setImageResource(elementId);
Log.d("CdImageid", ""+cdImageId);
closeDialog(view);
}
});
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
dialog = builder.create();
break;
default:
dialog = null;
}
/** onclick listner for the close button */
ImageView close = (ImageView) layout.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
dialog.dismiss();
}
});
return dialog;
}
any suggestions?
thanks!
There are two methods concerning this behaviour: setCancelable() and setCanceledOnTouchOutside() as you can see in the reference.

Categories