我正在尝试按照此youtube教程https://www.youtube.com/watch?v=x0I5vJfaRIU中的步骤创建一个登录/注册应用程序应用程序应该允许我在登录页面,注册页面和主要活动之间切换,这将允许我注销。当我按下注销按钮时,它会按照预期将我带到登录页面,当我单击TextView" tvRegisterHere"我应该被带到注册页面,但应用程序只是停止运行,这个错误消息出现在logcat"尝试调用虚方法' void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'在null对象引用"。我在Android Manifest中声明了Login和Register。我一直在寻找同样问题的例子,但修复其他问题的解决方案并没有解决我的问题。如果有人可以帮我解决我的问题,我将非常感激。
以下是我不同的.java文件
MainActivity.java
package com.example.john_000.ppmon;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button bLogout;
EditText etDisplayUsername, etDisplayCounty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
bLogout = (Button)findViewById(R.id.bLogout);
etDisplayCounty = (EditText)findViewById(R.id.etDisplayCounty);
etDisplayUsername = (EditText)findViewById(R.id.etDisplayUsername);
bLogout.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.bLogout:
startActivity(new Intent(this, Login.class));
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Login.java
package com.example.john_000.ppmon;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
import android.widget.TextView;
/**
* Created by jjohn_000 on 03/02/2016.
*/
public class Login extends ActionBarActivity implements View.OnClickListener{
Button bLogin;
EditText etUsername, etPassword;
TextView tvRegisterHere;
@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);
tvRegisterHere = (TextView)findViewById(R.id.tvRegisterHere);
bLogin.setOnClickListener(this);
tvRegisterHere.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.bLogin:
break;
case R.id.tvRegisterHere:
startActivity(new Intent(this, Register.class));
break;
}
}
Register.java
package com.example.john_000.ppmon;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by jjohn_000 on 03/02/2016.
*/
public class Register extends ActionBarActivity implements View.OnClickListener{
Button bRegister;
EditText etRegisterUsername, etRegisterPassword,etConfirmPassword, etCounty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etRegisterUsername = (EditText)findViewById(R.id.etUsername);
etRegisterPassword = (EditText)findViewById(R.id.etRegisterPassword);
etConfirmPassword = (EditText)findViewById(R.id.etConfirmPassword);
etCounty = (EditText)findViewById(R.id.etCounty);
bRegister = (Button)findViewById(R.id.bRegister);
bRegister.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.bRegister:
break;
}
}
}
logcat的
02-03 15:22:47.821 6849-6849/? I/art: Late-enabling -Xcheck:jni
02-03 15:22:48.299 6849-6873/com.example.john_000.ppmon D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-03 15:22:48.303 6849-6849/com.example.john_000.ppmon D/: HostConnection::get() New Host Connection established 0xb42d77a0, tid 6849
02-03 15:22:48.319 6849-6849/com.example.john_000.ppmon D/Atlas: Validating map...
02-03 15:22:48.439 6849-6873/com.example.john_000.ppmon D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
02-03 15:22:48.439 6849-6873/com.example.john_000.ppmon D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
02-03 15:22:48.448 6849-6873/com.example.john_000.ppmon D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so
02-03 15:22:48.468 6849-6873/com.example.john_000.ppmon D/: HostConnection::get() New Host Connection established 0xaf039510, tid 6873
02-03 15:22:48.503 6849-6873/com.example.john_000.ppmon I/OpenGLRenderer: Initialized EGL, version 1.4
02-03 15:22:48.591 6849-6873/com.example.john_000.ppmon D/OpenGLRenderer: Enabling debug mode 0
02-03 15:22:48.631 6849-6873/com.example.john_000.ppmon W/EGL_emulation: eglSurfaceAttrib not implemented
02-03 15:22:48.631 6849-6873/com.example.john_000.ppmon W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaf035860, error=EGL_SUCCESS
02-03 15:22:53.308 6849-6873/com.example.john_000.ppmon W/EGL_emulation: eglSurfaceAttrib not implemented
02-03 15:22:53.308 6849-6873/com.example.john_000.ppmon W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb427f7a0, error=EGL_SUCCESS
02-03 15:22:53.582 6849-6873/com.example.john_000.ppmon D/OpenGLRenderer: endAllStagingAnimators on 0xb4198580 (RippleDrawable) with handle 0xb42d7ba0
02-03 15:22:54.462 6849-6849/com.example.john_000.ppmon D/AndroidRuntime: Shutting down VM
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: FATAL EXCEPTION: main
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: Process: com.example.john_000.ppmon, PID: 6849
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.john_000.ppmon/com.example.john_000.ppmon.Register}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:151)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at com.example.john_000.ppmon.Register.onCreate(Register.java:28)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5990)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:151)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
02-03 15:22:54.463 6849-6849/com.example.john_000.ppmon E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:0)
我假设您在Register
活动中使用了错误的xml。这就是为什么您无法获得导致R.id.bRegister
的{{1}}。
NullPointerException
应该与setContentView(R.layout.activity_login);
活动中的setContentView(R.layout.activity_register);
类似。