Android Studio: java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference [TextView]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34861009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Studio: java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference [TextView]
提问by J2FX
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
I have initialised tvRegisterLink before setOnClickListener
I have declared tvRegisterLink before the method onCreate
There is no duplication of the tvRegisterLink ID
id tvRegisterLink 在同一个视图中
我在 setOnClickListener 之前初始化了 tvRegisterLink
我在方法 onCreate 之前声明了 tvRegisterLink
tvRegisterLink ID 没有重复
Any ideas or pointers in the right direction would be great !
任何朝着正确方向的想法或指示都会很棒!
Thanks.
谢谢。
回答by George Mulligan
You are never assigning bRegister
in the Register.onCreate
method before setting the click listener on it.
在设置点击侦听器之前,您永远不会bRegister
在Register.onCreate
方法中分配。
bRegister = (Button) findViewById(R.id.bRegister);
bRegister = (Button) findViewById(R.id.bRegister);
回答by Rafael Carlos
You are not instantiating Button bRegister;
in class Register, why it is giving error while receiving the click event.
您没有Button bRegister;
在类 Register 中实例化,为什么它在接收点击事件时出错。
回答by Shahar
Take a look at the error and the Register Class
看一下错误和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:尝试在空对象引用上调用虚拟方法“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);