I have a following Fragment:
public class FragmentSocial extends Fragment implements ActionBar.TabListener, OnClickListener
{
private Fragment mFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setContentView(R.layout.fragment_social);
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// relevant code...
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// relevant code...
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void onClick(View v) {
switch(v.getId())
{
case R.id.imgBtnFB:
Toast.makeText(this, "FB pressed", Toast.LENGTH_LONG).show();
break;
case R.id.imgBtnTwitter:
Toast.makeText(this, "Twitter pressed", Toast.LENGTH_LONG).show();
break;
}
}
I have a few image buttons in my fragment_social
layout. For now I just want to make a Toast message to see what button was pressed. However, if I use this
as context, I'm getting this error message:
The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (FragmentSocial, String, int)
I tried to change this
to FragmentSocial.this
, to FragmentSocial.this.getActivity()
, tried to create private Context mContext
and to instantiate it to mContext = (I tried various options here)
inside the onCreate method - but nothing worked. I either didn't have an error message, but also didn't see the Toast, or got other errors.
So how can I create a Toast here?
Thank you!
use:
getActivity().getBaseContext();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With