尝试使一个按钮加载另一个活动,然而点击监听器导致应用崩溃。

huangapple go评论120阅读模式
英文:

Trying to get a button to load another activity however the is OnClickListner crashing the app

问题

当点击注册按钮时,应用程序应加载另一个活动,但实际上它只会崩溃。尝试在这里1和这里2寻求帮助,但仍然无法解决。我还尝试了删除'implements View.OnClickListener',并按照示例中显示的方式使用它,但没有成功。还尝试将xml文件更改为LinearLayout,但也没有成功。提前致谢!

  1. public class LogInActivity extends AppCompatActivity implements View.OnClickListener {
  2. // 用户输入变量
  3. EditText emailAddressInput;
  4. EditText passwordInput;
  5. Button logInButton;
  6. Button registerButton;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. // 将布局设置为activity_login.xml
  11. setContentView(R.layout.activity_login);
  12. // 获取用户输入并设置到变量中
  13. emailAddressInput = (EditText)findViewById(R.id.emailAddressInput);
  14. passwordInput = (EditText)findViewById(R.id.passwordInput);
  15. logInButton = (Button)findViewById(R.id.logInButton);
  16. registerButton = (Button)findViewById(R.id.registerButton);
  17. logInButton.setOnClickListener(this);
  18. registerButton.setOnClickListener(this);
  19. }
  20. @Override
  21. public void onClick(View view) {
  22. Intent registerIntent = new Intent (LogInActivity.this, RegisterActivity.class);
  23. switch (view.getId()) {
  24. case R.id.logInButton:
  25. break;
  26. case R.id.registerButton:
  27. startActivity(registerIntent);
  28. break;
  29. }
  30. }
  31. }

编辑:
logcat日志:

2020-04-04 16:02:10.759 12718-12718/com.example.blooddonorsystem E/AndroidRuntime: 致命异常:主线程
进程:com.example.blooddonorsystem,PID:12718
java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.blooddonorsystem/com.example.blooddonorsystem.RegisterActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
导致原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)'
at com.example.blooddonorsystem.RegisterActivity.onCreate(RegisterActivity.java:56)
at android.app.Activity.performCreate(Activity.java:7825)
at android.app.Activity.performCreate(Activity.java:7814)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)。
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)。
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)。
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)。
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)。
at android.os.Handler.dispatchMessage(Handler.java:107)。
at android.os.Looper.loop(Looper.java:214)。
at android.app.ActivityThread.main(ActivityThread.java:7356)。
at java.lang.reflect.Method.invoke(Native Method)。
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)。
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)。

  1. <details>
  2. <summary>英文:</summary>
  3. When the register button is clicked the app should load another activity however it just crashes. Tried looking [here][1] and [here][2] for help but I still couldn&#39;t fix it. I also tried removing &#39;implements View.OnClickListner&#39; and using it how its shown in the examples and it didn&#39;t work. Also tried changing the xml file to a LinearLayout and that didn&#39;t work either. Thanks in advance!
  4. public class LogInActivity extends AppCompatActivity implements View.OnClickListener {
  5. //user input variables
  6. EditText emailAddressInput;
  7. EditText passwordInput;
  8. Button logInButton;
  9. Button registerButton;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. //set the layout to activity_login.xml
  14. setContentView(R.layout.activity_login);
  15. //get user inputs and set to the variables
  16. emailAddressInput = (EditText)findViewById(R.id.emailAddressInput);
  17. passwordInput = (EditText)findViewById(R.id.passwordInput);
  18. logInButton = (Button)findViewById(R.id.logInButton);
  19. registerButton = (Button)findViewById(R.id.registerButton);
  20. logInButton.setOnClickListener(this);
  21. registerButton.setOnClickListener(this);
  22. }
  23. @Override
  24. public void onClick(View view) {
  25. Intent registerIntent = new Intent (LogInActivity.this,RegisterActivity.class);
  26. switch (view.getId())
  27. {
  28. case R.id.logInButton:
  29. break;
  30. case R.id.registerButton:
  31. startActivity(registerIntent);
  32. break;
  33. }
  34. }
  35. }
  36. EDIT:
  37. the logcat:
  38. 2020-04-04 16:02:10.759 12718-12718/com.example.blooddonorsystem E/AndroidRuntime: FATAL EXCEPTION: main
  39. Process: com.example.blooddonorsystem, PID: 12718
  40. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.blooddonorsystem/com.example.blooddonorsystem.RegisterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method &#39;void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)&#39; on a null object reference
  41. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
  42. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
  43. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
  44. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
  45. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
  46. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
  47. at android.os.Handler.dispatchMessage(Handler.java:107)
  48. at android.os.Looper.loop(Looper.java:214)
  49. at android.app.ActivityThread.main(ActivityThread.java:7356)
  50. at java.lang.reflect.Method.invoke(Native Method)
  51. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
  52. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
  53. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method &#39;void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)&#39; on a null object reference
  54. at com.example.blooddonorsystem.RegisterActivity.onCreate(RegisterActivity.java:56)
  55. at android.app.Activity.performCreate(Activity.java:7825)
  56. at android.app.Activity.performCreate(Activity.java:7814)
  57. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
  58. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
  59. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)&#160;
  60. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)&#160;
  61. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)&#160;
  62. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)&#160;
  63. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)&#160;
  64. at android.os.Handler.dispatchMessage(Handler.java:107)&#160;
  65. at android.os.Looper.loop(Looper.java:214)&#160;
  66. at android.app.ActivityThread.main(ActivityThread.java:7356)&#160;
  67. at java.lang.reflect.Method.invoke(Native Method)&#160;
  68. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)&#160;
  69. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)&#160;
  70. [1]: http://Intent%20registerIntent%20=%20new%20Intent%20(LogInActivity.this,RegisterActivity.class);
  71. [2]: https://stackoverflow.com/questions/58830966/android-why-does-my-onclicklistner-crashing-my-app
  72. </details>
  73. # 答案1
  74. **得分**: 1
  75. 在您的`RegisterActivity`中,似乎您将适配器设置给了空的Spinner。请确保它具有正确的值,通常是一个包含字符串的ArrayAdapter。
  76. <details>
  77. <summary>英文:</summary>
  78. In your RegisterActivity, seems like the Spinner you are setting the adapter to is null. Make sure it has correct value, usually its an ArrayAdapter of String.
  79. </details>
  80. # 答案2
  81. **得分**: 0
  82. 错误不在LoginActivity中,在RegisterActivity中。在RegisterActivity中存在一个空指针异常,当您为微调器设置适配器时发生异常。
  83. <details>
  84. <summary>英文:</summary>
  85. The error is not in LoginActivity but in RegisterActivity.There is a null pointer exception in RegisterActivity when you are setting adapter to a spinner.
  86. </details>

huangapple
  • 本文由 发表于 2020年4月4日 22:57:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61029969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定