onClick 侦听器找不到我的一个按钮片段.初始化按钮时崩溃



所以当我启动我的应用程序时,它只是崩溃,因为我的第一个片段类中的按钮由于某种奇怪的原因而找不到。

当我删除案例 R.id.button_accept:程序运行良好,所以我认为这很可能是我对register_user_fragment中的 XML 所做的。那么,由于一个类中有多个布局,我需要以特定的方式调用按钮吗?

这是错误。

 Caused by: java.lang.IllegalStateException: Required view 'button_accept' with ID 2131296297 for field 'button_accept' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.

这是我的基本活动

public class BaseActivity extends AppCompatActivity implements View.OnClickListener {

//Fragments
Fragment usernameFragment;
Fragment passwordFragment;
//
FragmentManager fragmentManager;
@BindView(R.id.button_login)
Button button_login;
@BindView(R.id.button_accept) //CRASHES HERE.
Button button_accept;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //check if user is logged in or not
    setContentView(R.layout.activity_login);
    ButterKnife.bind(this);

    usernameFragment = new UsernameFragment();
    passwordFragment = new PasswordFragment();
    fragmentManager = getFragmentManager();
    button_login.setOnClickListener(this);
    button_accept.setOnClickListener(this);
    //setcontentview register page or whatever use is logged into
}
@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.button_login :
            Toast.makeText(this, "testds", Toast.LENGTH_SHORT).show();
            fragmentManager.beginTransaction()
                    .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
                    .replace(R.id.content_login, usernameFragment)
                    .commit();
            break;
        case R.id.button_accept :
            fragmentManager.beginTransaction()
                    .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
                    .replace(R.id.register_user_fragment, passwordFragment)
                    .commit();
            break;
    }
}

包含"button_accept"的 XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/ghostWhiteColor">


<android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/ghostWhiteColor"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/register_user_fragment"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="100dp">
        <TextView
            android:id="@+id/register_user_label_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/f_roboto_medium"
            android:gravity="center"
            android:text="Choose a username"
            android:textColor="@color/myBlack"
            android:textSize="25sp" />
        <TextView
            android:id="@+id/register_user_label_below"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/f_roboto_lightitalic"
            android:gravity="center"
            android:paddingTop="35dp"
            android:text="Your nickname can be changed."
            android:textColor="@color/myBlack"
            android:textSize="16sp" />
    </RelativeLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="200dp">
        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:id="@+id/register_username"
                android:layout_width="285dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:hint="@string/hint_name"
                android:singleLine="true" />
        </android.support.design.widget.TextInputLayout>

        <TextView
            android:id="@+id/label_tos"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:fontFamily="@font/f_roboto_lightitalic"
            android:gravity="center"
            android:text="@string/label_tos"
            android:textColor="@color/myBlack"
            android:textSize="@dimen/fui_heading_padding_bottom" />
        <Button
            android:id="@+id/button_accept"
            android:layout_width="125dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:layout_marginBottom="80dp"
            android:background="@drawable/oval"
            android:text="@string/btn_sign_up"
            android:textColor="@android:color/white" />
    </LinearLayout>

</FrameLayout>
</android.support.design.widget.CoordinatorLayout>

因为"button_accept"在您的片段布局中,而不是在您的活动布局:)中。

绑定活动布局中不存在的 UI 组件。

您刚刚混合了布局。 只需从框架布局中删除按钮accept_button即可。问题在于您在动态放置片段的布局父级中有按钮。因此,将按钮移到片段容器的外侧或上父级和外侧。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/ghostWhiteColor">


<android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/ghostWhiteColor"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/register_user_fragment"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="100dp">
        <TextView
            android:id="@+id/register_user_label_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/f_roboto_medium"
            android:gravity="center"
            android:text="Choose a username"
            android:textColor="@color/myBlack"
            android:textSize="25sp" />
        <TextView
            android:id="@+id/register_user_label_below"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/f_roboto_lightitalic"
            android:gravity="center"
            android:paddingTop="35dp"
            android:text="Your nickname can be changed."
            android:textColor="@color/myBlack"
            android:textSize="16sp" />
    </RelativeLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="200dp">
        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:id="@+id/register_username"
                android:layout_width="285dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:hint="@string/hint_name"
                android:singleLine="true" />
        </android.support.design.widget.TextInputLayout>

        <TextView
            android:id="@+id/label_tos"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:fontFamily="@font/f_roboto_lightitalic"
            android:gravity="center"
            android:text="@string/label_tos"
            android:textColor="@color/myBlack"
            android:textSize="@dimen/fui_heading_padding_bottom" />
    </LinearLayout>
</FrameLayout>
<Button
            android:id="@+id/button_accept"
            android:layout_width="125dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:layout_marginBottom="80dp"
            android:background="@drawable/oval"
            android:text="@string/btn_sign_up"
            android:textColor="@android:color/white" />
</android.support.design.widget.CoordinatorLayout>

相关内容

最新更新