Android – toast in onclick method in fragment
•
Android
I have the following clip:
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;
}
}
My fragment_ There are several image buttons in the social layout. Now I just want to make a toast message and see what button is pressed. However, if I use it as the context, I receive this error message: the method maketext (context, charsequence, int) in the toast type is not applicable to the parameter (fragmentsocial, string, int). I try to change it to fragmentsocial.this, Change to fragmentsocial. This. Getactivity(), try to create a private context mcontext in the oncreate method and instantiate it as mcontext = (I tried various options here) – but it has no effect. I either don't have an error message, don't see toast, or there are other errors
How can I create a toast here?
thank you!
resolvent:
use:
getActivity().getBaseContext();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码