onClick监听器无法找到我的一个按钮片段。初始化按钮时崩溃

时间:2017-11-19 18:33:00

标签: java android xml android-fragments

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

当我删除了 case 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>

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您刚刚将布局混合在一起。只需从框架布局中删除Button 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>