简体   繁体   中英

Android Studio: java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference [TextView]

I have searched for an answer that works for me but have not come across anything that helped.

My problem is that I am linking a TextView to another activity, however when clicked on throws a NullPointerException

This is the error:

01-18 07:03:41.882 14021-14035/com.j2fx.msc_driverlogin E/Surface:     getSlotFromBufferLocked: unknown buffer: 0xab7d7650
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime: FATAL EXCEPTION: main
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime: Process: com.j2fx.msc_driverlogin, PID: 14021
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime:     java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.j2fx.msc_driverlogin/com.j2fx.msc_driverlogin.RegisterNew}:    java.lang.NullPointerException: Attempt to invoke virtual method 'void  android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a  null object reference
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
(quite a lot more stack trace but probably not relevant)

Here is the Login.class

package com.j2fx.msc_driverlogin;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Login extends AppCompatActivity implements View.OnClickListener {

    Button bLogin;
    EditText etUsername, etPassword;
    TextView tvRegisterLink;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        etUsername = (EditText) findViewById(R.id.etUsername);
        etPassword = (EditText) findViewById(R.id.etPassword);
        bLogin = (Button) findViewById(R.id.bLogin);
        tvRegisterLink = (TextView) findViewById(R.id.tvRegisterLink);

        bLogin.setOnClickListener(this);
        tvRegisterLink.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bLogin:
                // will add login code here
                break;
            case R.id.tvRegisterLink:
                startActivity(new Intent(this, Register.class));
                break;
        }
    }
}

Here is the Activity_Login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:orientation="vertical"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Driver ID"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:id="@+id/etUsername"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberPassword"
        android:layout_marginBottom="10dp"
        android:id="@+id/etPassword"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/bLogin"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold"
        android:text="Register new account"
        android:id="@+id/tvRegisterLink"/>

</LinearLayout>

Here is the Register.class

package com.j2fx.msc_driverlogin;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Register extends AppCompatActivity implements View.OnClickListener {

    Button bRegister;
    EditText etName, etAddress, etDateOfBirth, etVehicleReg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        etName = (EditText) findViewById(R.id.etName);
        etAddress = (EditText) findViewById(R.id.etAddress);
        etDateOfBirth = (EditText) findViewById(R.id.etDateOfBirth);
        etVehicleReg = (EditText) findViewById(R.id.etVehicleReg);

        bRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bRegister:


                break;
        }
    }
}

And finaly the Activity_Register.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:orientation="vertical"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:id="@+id/etName"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Address"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:id="@+id/etAddress"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Date of Birth"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="date"
        android:layout_marginBottom="10dp"
        android:id="@+id/etDateOfBirth"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Vehicle Reg"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:layout_marginBottom="10dp"
        android:id="@+id/etVehicleReg"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Register"
        android:id="@+id/bRegister"/>

</LinearLayout>

I have checked thoroughly and cannot see where I have gone wrong, although I am new to this so possibly missing something trivial.

Some points:

  • The id tvRegisterLink is within the same view

  • I have initialised tvRegisterLink before setOnClickListener

  • I have declared tvRegisterLink before the method onCreate

  • There is no duplication of the tvRegisterLink ID

Any ideas or pointers in the right direction would be great !

Thanks.

在设置点击监听器之前,您永远不会在Register.onCreate方法中分配bRegister

bRegister = (Button) findViewById(R.id.bRegister);

You are not instantiating Button bRegister; in class Register, why it is giving error while receiving the click event.

Take a look at the error and the Register Class

ava.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object referenc

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    etName = (EditText) findViewById(R.id.etName);
    etAddress = (EditText) findViewById(R.id.etAddress);
    etDateOfBirth = (EditText) findViewById(R.id.etDateOfBirth);
    etVehicleReg = (EditText) findViewById(R.id.etVehicleReg);

    bRegister.setOnClickListener(this);
}

bRegister is not initialised.

just add

 bRegister = (Button) findViewById(R.id.bRegister);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM