Android 为什么findByViewId返回null?
我觉得这是一个大脑受损的问题,但就我个人而言,我看不出有什么不对 我的活动具有以下按钮定义(在单独的LinearLayout类中) emailSignInButton工作正常,而forgotPasswordButton返回null。我生病了,或者这对我来说很明显 我试着抛开logcat并查看信息,但没有发现任何证据 以下是整个xml文件:Android 为什么findByViewId返回null?,android,android-activity,Android,Android Activity,我觉得这是一个大脑受损的问题,但就我个人而言,我看不出有什么不对 我的活动具有以下按钮定义(在单独的LinearLayout类中) emailSignInButton工作正常,而forgotPasswordButton返回null。我生病了,或者这对我来说很明显 我试着抛开logcat并查看信息,但没有发现任何证据 以下是整个xml文件: <!-- Login progress --> <ProgressBar android:id="@+id/login_progre
<!-- 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>
我试着把这个按钮和另一个按钮放在同一条直线上,看看它是否有不同。它没有。我认为问题在于,在初始化视图之前初始化方法。 因此,请尝试以下方法:
@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;
}
});
}
我还建议,你应该用黄油刀
这将清理您的代码明白了!在某个时候,我为这个活动创建了多个布局,用于其他大小的设备。我不知道为什么。因此,代码运行的布局与主布局不同 我将我的按钮添加到我所有的布局中,它似乎已经解决了这个问题
睡了一夜好觉后感觉好多了。似乎我已经清醒了。你说的“在单独的线性布局课程中”是什么意思?您是如何在活动中加载两个线性布局的?您所说的分开线性布局是什么意思?请添加完整的xml文件和java活动代码。@AlexTa,我的活动在垂直线性布局中包含多个水平线性布局。此代码在活动中,因此是的,它们都已加载。请将日志cat也发送给我们好吗?现在一切似乎都很好@ThomNo,不是这样。我不确定你认为重新安排代码会有什么帮助。正如我指出的,之前的按钮已正确初始化。
<!-- 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>
@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);
}
@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;
}
});
}