Reputation: 163
I have a fragment within my activity_main. This fragment contains a button (ViewButton). When i press this button I would like for it to bring up a pop Up dialog. I have the following code below, the issue is that i get two errors which i dont seem to understand very well: new AlertDialog.Builder(this); getLayoutInflater();
They are both coming up as undefined. My guess is that i need to put 'view' or 'this' somewhere, or extend the Activity? But i cant understand exactly the problem.
public class CurrentFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.current_fragment, container, false);
Button ViewButton = (Button)view.findViewById(R.id.ViewButton);
ViewButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
//public void ShowPUDialog()
{
AlertDialog.Builder PUHelpBuilder = new AlertDialog.Builder(this);
PUHelpBuilder.setTitle("Enter Pick Up Address");
LayoutInflater inflater = getLayoutInflater();
View DialogLayout = inflater.inflate(R.layout.pudialog, null);
PUHelpBuilder.setView(DialogLayout);
PUHelpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
}
});
AlertDialog helpDialog = PUHelpBuilder.create();
helpDialog.show();
}
});
}
return view;
}
}
Upvotes: 0
Views: 771
Reputation: 44571
Use either getActivity()
or v.getContext()
instead of this
. Since you are inside of an OnClickListener
(Anonymous inner class) this
refers to the OnClickListener
instead of the proper Context
. Try something like
AlertDialog.Builder PUHelpBuilder = new AlertDialog.Builder(v.getContext());
You need a Context
for getLayoutInflater()
also since it is an Activity
method so try the same thing
v.getContext().getLayoutInflater();
Edit
Look at the Activity Docs...it extends Context
which means it has it's own Context
and is why you can use this
when inside of an Acitivity
method. But as I said, in your onClick()
you are actually inside of an anonymous inner-class so this
no longer refers to the Activity Context
.
Upvotes: 1