Java 尝试调用虚拟方法';void android.widget.Button.setOnClickListener(android.view.view$OnClickListener)和#x27;关于空对象引用
我想做的就是点击一个按钮,开始一个新的意图。这是我的代码(我删除了不相关的部分):Java 尝试调用虚拟方法';void android.widget.Button.setOnClickListener(android.view.view$OnClickListener)和#x27;关于空对象引用,java,android,nullpointerexception,onclicklistener,Java,Android,Nullpointerexception,Onclicklistener,我想做的就是点击一个按钮,开始一个新的意图。这是我的代码(我删除了不相关的部分): 活动\u login.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height
活动\u login.xml
:
<LinearLayout 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"
android:gravity="center_horizontal"
tools:context="com.example.android.altro.LoginActivity">
<!-- 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">
<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:textColor="@color/colorWhite"
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>
<Button
android:id="@+id/email_sign_up_button"
style="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_button"
android:onClick="registerNewUser"
android:text="@string/action_sign_up"
android:textAllCaps="false" />
</LinearLayout>
</ScrollView>
</LinearLayout>
我还尝试通过查找视图,然后创建意图(代码在onCreate()
方法中注释掉。这也给了我同样的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.altro/com.example.android.altro.RegisterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
您只有一个id为电子邮件注册按钮的
按钮
请检查您的布局
。同时尝试更换
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
与
这不是问题,我已经删除了按钮代码以使其简短。就在那里。请精简您的代码,并更具体地说明问题所在。这将使我们更容易帮助您。发布您的注册活动
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_up_button);