未检测到Android按钮,并在尝试检索时在onCreate中返回null

时间:2017-04-22 20:59:09

标签: java android xml onclick onclicklistener

我的XML中有一个按钮,我在活动的java代码的onCreate方法中检索。但是,当我去运行应用程序时,会设置一个空指针异常。

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.chargerpoints.LogonActivity">

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

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/password"
        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"/>

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

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/register_button"
        style="?android:textAppearanceSmall"
        android:layout_marginTop="8dp"
        android:text="@string/action_register"
        android:textStyle="bold"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/forgot_password_button"
        style="?android:textAppearanceSmall"
        android:layout_marginTop="16dp"
        android:text="Forgot Password"
        android:textStyle="bold"/>

</LinearLayout>

有问题的按钮是id为#34; sign_in_button&#34;的按钮。这是java代码中的onCreate方法:     private TextView emailTextView;     私有TextView passwordTextView;

private Button signInButton;
private Button registerButton;
private Button forgotPasswordButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    realm = Realm.getDefaultInstance();

    emailTextView = (TextView) findViewById(R.id.email);
    passwordTextView = (TextView) findViewById(R.id.password);

    signInButton = (Button) findViewById(R.id.sign_in_button);
    signInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(LogonActivity.this, signIn(),
                    Toast.LENGTH_LONG).show();
        }
    });

    registerButton = (Button) findViewById(R.id.register_button);
    registerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(LogonActivity.this, register(),
                    Toast.LENGTH_LONG).show();
        }
    });

    forgotPasswordButton = (Button) findViewById(R.id.forgot_password_button);
}

这是我得到的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: com.example.android.chargerpoints, PID: 7276
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.chargerpoints/com.example.android.chargerpoints.LogonActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:154)
                                                 at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                 at com.example.android.chargerpoints.LogonActivity.onCreate(LogonActivity.java:42)
                                                 at android.app.Activity.performCreate(Activity.java:6662)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                 at android.os.Looper.loop(Looper.java:154) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

如果有人可以帮助我,这将是惊人的。我是这个东西的新手,这个问题让我陷入困境,这可能只是我错过的一件小事。谢谢!

2 个答案:

答案 0 :(得分:2)

首先需要在activity / fragment中扩展布局。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.<xml file name>);

    ....

答案 1 :(得分:0)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//without this line your activity class doesn't know which xml file you wanna use
    setContentView(R.layout.yourlayoutname);

    //and only after that you can retrive names
    bnt = (Button) findViewById(R.id.btn)'
    }