需要帮助以使Toast出现在选项卡片段上

问题描述

一个EditText留空时,我试图让Toast出现在选项卡式片段中。

这是我的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);        
    }
}

我的选项卡式片段可以在以下代码中正常工作:

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 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,false);
    }
}

解决方法

您没有访问Fragment布局的视图,因此您在onCreateView()中返回的布局没有附加逻辑。您应该将片段设置为如下所示:

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;
    }
}
,

我很惊讶为什么您的代码没有出现 true rendevous 之类的错误,因为

首先,您需要在显示Toast之前更改活动,其次,在初始化视图之后又要夸大布局,并且无法正确获取上下文,要实现的正确方法是使用 {{1} } 这样

VIEW DOES NOT EXIST

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...