简体   繁体   English

Android Studio:java.lang.NullPointerException:尝试在空对象引用[TextView]上调用虚拟方法

[英]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 我的问题是我将TextView链接到另一个活动,但是单击时会抛出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 这是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 这是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 这是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 最后是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 id tvRegisterLink在同一视图中

  • I have initialised tvRegisterLink before setOnClickListener 我已经在setOnClickListener之前初始化了tvRegisterLink

  • I have declared tvRegisterLink before the method onCreate 我在方法onCreate之前声明了tvRegisterLink

  • There is no duplication of the tvRegisterLink ID 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; 您没有实例化Button bRegister; in class Register, why it is giving error while receiving the click event. 在类Register中,为什么它在收到click事件时给出错误信息。

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 ava.lang.NullPointerException:尝试在null对象Referenc上调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'

@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. bRegister未初始化。

just add 只需添加

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio java.lang.NullPointerException:尝试调用虚拟方法 - Android Studio java.lang.NullPointerException: Attempt to invoke virtual method java.lang.NullPointerException:尝试在空对象引用上调用虚方法,而它不为空 - java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference, while its not null java.lang.NullPointerException:尝试在 oncreate 方法中对空对象引用调用虚拟方法 - java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference in oncreate method java.lang.NullPointerException:尝试对空对象引用调用虚拟方法。 应用程序在运行时崩溃 - java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference. App crashes on run java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法… - java.lang.NullPointerException: Attempt to invoke virtual method … on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“RecyclerView$ViewHolder.shouldIgnore()” - java.lang.NullPointerException: Attempt to invoke virtual method 'RecyclerView$ViewHolder.shouldIgnore()' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚方法&#39;ActionBar.setNavigationMode(int)&#39; - java.lang.NullPointerException: Attempt to invoke virtual method 'ActionBar.setNavigationMode(int)' on a null object reference 致命异常:java.lang.NullPointerException:尝试在空对象引用onClickItem上调用虚方法void - FATAL EXCEPTION: java.lang.NullPointerException: Attempt to invoke virtual method void on a null object reference onClickItem java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 - java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法FloatingActionButton.setImageResource(int)&#39; - java.lang.NullPointerException: Attempt to invoke virtual method FloatingActionButton.setImageResource(int)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM