需要帮助让 Toast 出现在 Tab Fragment 上
Need help getting a Toast to appear on a Tab Fragment
我正在尝试让 Toast 在 EditText 留空时出现在选项卡式片段中。
这是我的 MainActivity 的 onCreate:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
}
我的 Tabbed Fragment 使用此代码可以正常工作:
public class SignupFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_signup, container, false);
}
}
但是这样创建 Toast 时不起作用:
public class SignupFragment extends Fragment {
EditText textFillCheck;
Button submitCheck;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
submitCheck = (Button) submitCheck.findViewById(R.id.signupBtn);
textFillCheck = (EditText) textFillCheck.findViewById(R.id.signupFirstName);
submitCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TextUtils.isEmpty(textFillCheck.getText().toString())) {
Intent intent = new Intent(getActivity(), SignupFragment.class);
startActivity(intent);
Toast.makeText(getActivity(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
}
else{Toast.makeText(getActivity(), textFillCheck.getText().toString(), Toast.LENGTH_LONG).show();
}
}
});
return inflater.inflate(R.layout.fragment_signup, container, false);
}
}
我很惊讶为什么你的代码没有像 VIEW DOES NOT EXIST
或 NULL POINTER EXCEPTION
这样的错误,因为
首先,您在显示 Toast 之前更改了 activity,其次,您在视图初始化之后膨胀了布局并且没有正确获取上下文,正确的方法是使用 OnViewCreated()
像这样
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
submitCheck = (Button) view.findViewById(R.id.signupBtn);
textFillCheck = (EditText) view.findViewById(R.id.signupFirstName);
submitCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TextUtils.isEmpty(textFillCheck.getText().toString())) {
Toast.makeText(getActivity(), "Please fill in all fields",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), SignupFragment.class);
startActivity(intent);
}
else{
Toast.makeText(getActivity(), textFillCheck.getText().toString(),
Toast.LENGTH_LONG).show();
}
}
});
}
您没有访问 Fragment 布局的视图,因此您在 onCreateView()
中返回的布局没有附加您的逻辑。您应该将 Fragment 设置为如下所示:
public class SignupFragment extends Fragment {
EditText textFillCheck;
Button submitCheck;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
// Store a reference to your Fragment's inflated layout
View root = inflater.inflate(R.layout.fragment_signup, container, false);
// Use the reference to access your Fragment's views
submitCheck = (Button) root.findViewById(R.id.signupBtn);
textFillCheck = (EditText) root.findViewById(R.id.signupFirstName);
submitCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(TextUtils.isEmpty(textFillCheck.getText().toString())) {
Toast.makeText(getActivity(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(), textFillCheck.getText().toString(), Toast.LENGTH_LONG).show();
}
}
});
// Return the inflated layout
return root;
}
}
我正在尝试让 Toast 在 EditText 留空时出现在选项卡式片段中。
这是我的 MainActivity 的 onCreate:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
}
我的 Tabbed Fragment 使用此代码可以正常工作:
public class SignupFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_signup, container, false);
}
}
但是这样创建 Toast 时不起作用:
public class SignupFragment extends Fragment {
EditText textFillCheck;
Button submitCheck;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
submitCheck = (Button) submitCheck.findViewById(R.id.signupBtn);
textFillCheck = (EditText) textFillCheck.findViewById(R.id.signupFirstName);
submitCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TextUtils.isEmpty(textFillCheck.getText().toString())) {
Intent intent = new Intent(getActivity(), SignupFragment.class);
startActivity(intent);
Toast.makeText(getActivity(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
}
else{Toast.makeText(getActivity(), textFillCheck.getText().toString(), Toast.LENGTH_LONG).show();
}
}
});
return inflater.inflate(R.layout.fragment_signup, container, false);
}
}
我很惊讶为什么你的代码没有像 VIEW DOES NOT EXIST
或 NULL POINTER EXCEPTION
这样的错误,因为
首先,您在显示 Toast 之前更改了 activity,其次,您在视图初始化之后膨胀了布局并且没有正确获取上下文,正确的方法是使用 OnViewCreated()
像这样
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
submitCheck = (Button) view.findViewById(R.id.signupBtn);
textFillCheck = (EditText) view.findViewById(R.id.signupFirstName);
submitCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TextUtils.isEmpty(textFillCheck.getText().toString())) {
Toast.makeText(getActivity(), "Please fill in all fields",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), SignupFragment.class);
startActivity(intent);
}
else{
Toast.makeText(getActivity(), textFillCheck.getText().toString(),
Toast.LENGTH_LONG).show();
}
}
});
}
您没有访问 Fragment 布局的视图,因此您在 onCreateView()
中返回的布局没有附加您的逻辑。您应该将 Fragment 设置为如下所示:
public class SignupFragment extends Fragment {
EditText textFillCheck;
Button submitCheck;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
// Store a reference to your Fragment's inflated layout
View root = inflater.inflate(R.layout.fragment_signup, container, false);
// Use the reference to access your Fragment's views
submitCheck = (Button) root.findViewById(R.id.signupBtn);
textFillCheck = (EditText) root.findViewById(R.id.signupFirstName);
submitCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(TextUtils.isEmpty(textFillCheck.getText().toString())) {
Toast.makeText(getActivity(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(), textFillCheck.getText().toString(), Toast.LENGTH_LONG).show();
}
}
});
// Return the inflated layout
return root;
}
}