我正在尝试从我的片段中创建一个Toast。我一直在互联网上搜索几个小时,我遇到的所有东西似乎都没有用。
information.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast toast = Toast.makeText(getActivity().getBaseContext(), "This information will not be published in the world wide web, but will be saved on your own device instead", Toast.LENGTH_SHORT);
toast.show();
toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0);
}
});
&#39;的 getActivity &#39;是红色的。在悬停时,它说:&#34; 无法解决方法&#39; getActivity()&#39;&#34;。 如果我删除了getActivity,并且仅使用 getBaseContext()或 getApplicationContext(),则表示无法从静态上下文中引用非静态方法< / strong>即可。任何人都可以教我如何创建这个特定的Toast吗?
我使用了Android Studio中的标准图库标签应用程序。 这是我的代码(你会在最底层找到它):
public class ApplicationFragments extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
//get widgets voor alle functies
private static View rootView;
//variable
private static int section;
//fragment1
static ImageButton information;
static int addOneSex;
static int addOneWeight;
static int addOneAge;
static ImageView plusSex;
static ImageView plusWeight;
static ImageView plusAge;
static TextView sex;
static TextView weight;
static TextView age;
//fragment2
static int addOneHour;
static int addOneBeer;
static int addOneWine;
static int addOneShot;
static ImageView plusBeers;
static ImageView plusWines;
static ImageView plusShots;
static TextView plusHours;
static TextView amountOfHours;
static TextView amountOfBeers;
static TextView amountOfWines;
static TextView amountOfShots;
//fragment3
//all fragments
static Button btnNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
//verander animatie
overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.fade_out);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
//bij het creëren van een tab
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_information, container, false);
//set xml
section = getArguments().getInt(ARG_SECTION_NUMBER);
if (section == 1) {
rootView = inflater.inflate(R.layout.fragment_information, container, false);
GenerateFragment1();
}
if (section == 2) {
rootView = inflater.inflate(R.layout.fragment_drinks, container, false);
GenerateFragment2();
}
if (section == 3) {
rootView = inflater.inflate(R.layout.fragment_calculate, container, false);
GenerateFragment3();
}
return rootView;
}
}
//geef de juiste pagina terug
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
//hoeveelheid pagina's
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
//fragment 1
public static void GenerateFragment1(){
information = (ImageButton) rootView.findViewById(R.id.imgBtnInfo);
sex = (TextView) rootView.findViewById(R.id.etSex);
weight = (TextView) rootView.findViewById(R.id.etWeight);
age = (TextView) rootView.findViewById(R.id.etAge);
plusSex = (ImageView) rootView.findViewById(R.id.btnSex);
plusWeight = (ImageView) rootView.findViewById(R.id.btnWeight);
plusAge = (ImageView) rootView.findViewById(R.id.btnAge);
btnNext = (Button) rootView.findViewById(R.id.btnNext);
plusSex.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addOneSex = Integer.parseInt(sex.getText().toString());
addOneSex++;
sex.setText(Integer.toString(addOneSex));
}
});
plusWeight.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addOneWeight = Integer.parseInt(weight.getText().toString());
addOneWeight++;
weight.setText(Integer.toString(addOneWeight));
}
});
plusAge.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addOneAge = Integer.parseInt(age.getText().toString());
addOneAge++;
age.setText(Integer.toString(addOneAge));
}
});
information.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast toast = Toast.makeText(getActivity().getBaseContext(), "This information will not be published in the world wide web, but will be saved on your own device instead", Toast.LENGTH_SHORT);
toast.show();
toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0);
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//to the next fragment
}
});
}
另外,我想知道如何让应用程序转到按钮上的下一页(btnNext)。
感谢您的时间和帮助!
答案 0 :(得分:0)
您是否有某个存储在活动中的片段列表或片段管理器?如果是这样,您可以从列表中获取下一个片段并使用片段管理器将其切换出来。关于toast getActivity()问题:如果我理解正确,ApplicationFragments是你的主要活动?如果是这样,为什么不调用{{1}}而不是getActivity()?