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

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

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

问题

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

public class LogInActivity extends AppCompatActivity implements View.OnClickListener {

    // 用户输入变量
    EditText emailAddressInput;
    EditText passwordInput;
    Button logInButton;
    Button registerButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 将布局设置为activity_login.xml
        setContentView(R.layout.activity_login);

        // 获取用户输入并设置到变量中
        emailAddressInput = (EditText)findViewById(R.id.emailAddressInput);
        passwordInput = (EditText)findViewById(R.id.passwordInput);
        logInButton = (Button)findViewById(R.id.logInButton);
        registerButton = (Button)findViewById(R.id.registerButton);

        logInButton.setOnClickListener(this);
        registerButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent registerIntent = new Intent (LogInActivity.this, RegisterActivity.class);

        switch (view.getId()) {
            case R.id.logInButton:
                break;
            case R.id.registerButton:
                startActivity(registerIntent);
                break;
        }
    }
}

编辑:
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)。


<details>
<summary>英文:</summary>

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!

    public class LogInActivity extends AppCompatActivity implements View.OnClickListener {

    //user input variables
    EditText emailAddressInput;
    EditText passwordInput;
    Button logInButton;
    Button registerButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //set the layout to activity_login.xml
        setContentView(R.layout.activity_login);

        //get user inputs and set to the variables
        emailAddressInput = (EditText)findViewById(R.id.emailAddressInput);
        passwordInput = (EditText)findViewById(R.id.passwordInput);
        logInButton = (Button)findViewById(R.id.logInButton);
        registerButton = (Button)findViewById(R.id.registerButton);

        logInButton.setOnClickListener(this);
        registerButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent registerIntent = new Intent (LogInActivity.this,RegisterActivity.class);

        switch (view.getId())
        {
            case R.id.logInButton:
                break;
            case R.id.registerButton:
                startActivity(registerIntent);
                break;
        }
    }
}

EDIT:
the logcat:

2020-04-04 16:02:10.759 12718-12718/com.example.blooddonorsystem E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.blooddonorsystem, PID: 12718
    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
        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)
     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
        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)&#160;
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)&#160;
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)&#160;
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)&#160;
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)&#160;
        at android.os.Handler.dispatchMessage(Handler.java:107)&#160;
        at android.os.Looper.loop(Looper.java:214)&#160;
        at android.app.ActivityThread.main(ActivityThread.java:7356)&#160;
        at java.lang.reflect.Method.invoke(Native Method)&#160;
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)&#160;
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)&#160;

  [1]: http://Intent%20registerIntent%20=%20new%20Intent%20(LogInActivity.this,RegisterActivity.class);
  [2]: https://stackoverflow.com/questions/58830966/android-why-does-my-onclicklistner-crashing-my-app

</details>


# 答案1
**得分**: 1

在您的`RegisterActivity`中,似乎您将适配器设置给了空的Spinner。请确保它具有正确的值,通常是一个包含字符串的ArrayAdapter。

<details>
<summary>英文:</summary>

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.

</details>



# 答案2
**得分**: 0

错误不在LoginActivity中,在RegisterActivity中。在RegisterActivity中存在一个空指针异常,当您为微调器设置适配器时发生异常。

<details>
<summary>英文:</summary>

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. 

</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:

确定