为什么findByViewId返回null?

时间:2017-06-01 18:19:57

标签: android android-activity

我觉得这是一个大脑受损的问题,但对于我的生活,我无法看出错误。

我的活动具有以下按钮定义(在单独的LinearLayout类中)。

            <Button
                android:id="@+id/email_sign_in_button"
                style="?android:textAppearanceSmall"
                android:layout_width="250dp"
                android:layout_height="match_parent"
                android:layout_marginTop="16dp"
                android:text="@string/action_sign_in"
                android:textStyle="bold"
                />

            <Button
                android:id="@+id/forgot_password"
                style="?android:textAppearanceSmall"
                android:layout_gravity="center"
                android:layout_width="250dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="125dp"
                android:layout_marginTop="16dp"
                android:text="@string/action_forgot_password"
                android:textStyle="bold"
                />

然后我使用此代码:

    emailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
    emailSignInButton.setOnClickListener(loginClickListener);

    forgotPasswordButton = (Button) findViewById(R.id.forgot_password);
    forgotPasswordButton.setOnClickListener(new ForgotPasswordListener());

并且emailSignInButton工作正常,而forgotPasswordButton返回null。我生病了,或者这对我来说可能很明显......

我尝试转储logcat并查看信息,但没有发现任何证据。

这是整个xml文件:

<!-- Login progress -->
<ProgressBar
    android:id="@+id/login_progress"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:visibility="gone"/>

<ScrollView
    android:id="@+id/login_form"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/email_login_form"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView android:id="@+id/login"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:gravity="center"
                  android:text="@string/login"
                  android:textColor="@color/colorPrimary"
                  android:textSize="24sp"
            />

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <AutoCompleteTextView
                android:id="@+id/email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/prompt_email"
                android:inputType="textEmailAddress"
                android:maxLines="1"
                android:singleLine="true"/>

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/prompt_password"
                android:imeActionId="@+id/login"
                android:imeActionLabel="@string/action_sign_in_short"
                android:imeOptions="actionUnspecified"
                android:inputType="textPassword"
                android:maxLines="1"
                android:singleLine="true"/>

        </android.support.design.widget.TextInputLayout>


        <android.support.design.widget.TextInputLayout
            android:id="@+id/confirmPasswordLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone">

            <EditText
                android:id="@+id/confirmPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/prompt_confirm_password"
                android:imeActionId="@+id/login"
                android:imeActionLabel="@string/action_sign_in_short"
                android:imeOptions="actionUnspecified"
                android:inputType="textPassword"
                android:maxLines="1"
                android:singleLine="true"
                />

        </android.support.design.widget.TextInputLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <Button
                android:id="@+id/email_sign_in_button"
                style="?android:textAppearanceSmall"
                android:layout_width="250dp"
                android:layout_height="match_parent"
                android:layout_marginTop="16dp"
                android:text="@string/action_sign_in"
                android:textStyle="bold"
                />

            <Button
                android:id="@+id/create_account_button"
                style="?android:textAppearanceSmall"
                android:layout_width="250dp"
                android:layout_height="match_parent"
                android:layout_marginTop="16dp"
                android:text="@string/action_create_account"
                android:textStyle="bold"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <Button
                android:id="@+id/forgot_password"
                style="?android:textAppearanceSmall"
                android:layout_gravity="center"
                android:layout_width="250dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="125dp"
                android:layout_marginTop="16dp"
                android:text="@string/action_forgot_password"
                android:textStyle="bold"
                />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

整个onCreate()方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    // Set up the login form.
    emailView = (AutoCompleteTextView) findViewById(R.id.email);
    populateAutoComplete();

    loginTitle = (TextView) findViewById(R.id.login);

    passwordView = (EditText) findViewById(R.id.password);
    passwordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
            if (id == R.id.login || id == EditorInfo.IME_NULL) {
                attemptLogin();
                return true;
            }
            return false;
        }
    });

    confirmPasswordView = (EditText) findViewById(R.id.confirmPassword);

    confirmPasswordLayout = (LinearLayout) findViewById(R.id.confirmPasswordLayout);
    mainLayout = (LinearLayout) findViewById(R.id.mainLayout);

    LoginClickListener loginClickListener = new LoginClickListener();

    createAccountButton = (Button) findViewById(R.id.create_account_button);
    createAccountButton.setOnClickListener(loginClickListener);

    emailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
    emailSignInButton.setOnClickListener(loginClickListener);

    forgotPasswordButton = (Button) findViewById(R.id.forgot_password);
    forgotPasswordButton.setOnClickListener(new ForgotPasswordListener());

    loginFormView = findViewById(R.id.login_form);
    progressView = findViewById(R.id.login_progress);
}

我尝试将此按钮放在与另一个按钮相同的LinearLayout中,以查看它是否有所不同。它没有。

2 个答案:

答案 0 :(得分:0)

我认为问题是,在初始化Views之前初始化方法。 所以试试这个:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    // Set up the login form.
    emailView = (AutoCompleteTextView) findViewById(R.id.email);
    confirmPasswordView = (EditText) findViewById(R.id.confirmPassword);
    confirmPasswordLayout = (LinearLayout)  findViewById(R.id.confirmPasswordLayout);
    mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
    LoginClickListener loginClickListener = new LoginClickListener();
    createAccountButton = (Button) findViewById(R.id.create_account_button);
    createAccountButton.setOnClickListener(loginClickListener);
    emailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
    emailSignInButton.setOnClickListener(loginClickListener);
    forgotPasswordButton = (Button) findViewById(R.id.forgot_password);
    forgotPasswordButton.setOnClickListener(new ForgotPasswordListener());
    loginFormView = findViewById(R.id.login_form);
    progressView = findViewById(R.id.login_progress);
    loginTitle = (TextView) findViewById(R.id.login);
    passwordView = (EditText) findViewById(R.id.password);
    populateAutoComplete();
    passwordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
            if (id == R.id.login || id == EditorInfo.IME_NULL) {
                attemptLogin();
                return true;
            }
            return false;
        }
    });

}

我也建议,你应该使用Butterknife LINK

这将清理您的代码

答案 1 :(得分:0)

知道了!我曾在某些时候为其他设备大小的此活动创建了多个布局。我不知道为什么。所以代码运行的布局与主代码不同。

我将按钮添加到我的所有布局中,似乎解决了问题。

在一夜好眠之后感觉好多了。似乎已经清除了我的头脑。