Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 应用程序在活动之间传递数据时崩溃,空指针异常_Java_Android - Fatal编程技术网

Java 应用程序在活动之间传递数据时崩溃,空指针异常

Java 应用程序在活动之间传递数据时崩溃,空指针异常,java,android,Java,Android,我是Android开发的新手,抱歉,如果这是一个基本问题,我应该能够自己解决。我已经阅读了许多其他问题,这些问题是我如何在我能想到的任何地方启动开关的,但我仍然没有修复它 我正在做一个国际象棋游戏,如果用户在一个活动中把一个开关切换到“开”的位置,就可以记录游戏 错误 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.Switch.isChecke

我是Android开发的新手,抱歉,如果这是一个基本问题,我应该能够自己解决。我已经阅读了许多其他问题,这些问题是我如何在我能想到的任何地方启动开关的,但我仍然没有修复它

我正在做一个国际象棋游戏,如果用户在一个活动中把一个开关切换到“开”的位置,就可以记录游戏

错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
'boolean android.widget.Switch.isChecked()' on a null object reference at
com.android51.HomeActivity.toRecord(HomeActivity.java:43)
这就是我在国际象棋活动开始时传递该值的方式

public void toChess(View view){
    Intent myIntent = new Intent(HomeActivity.this, Chess.class);
    Bundle bundle = getIntent().getExtras();
    Boolean recordState = bundle.getBoolean("recordState");
    HomeActivity.this.startActivity(myIntent);
}

public void toRecord(View view){
    Intent myIntent = new Intent(HomeActivity.this, RecordActivity.class);
    Switch recordSwitch = (Switch) findViewById(R.id.toggle_record);

    Boolean recordState = recordSwitch.isChecked(); <---- NULL POINTER, WHY?

    Bundle bundle = new Bundle();
    bundle.putBoolean("recordState", recordState);
    myIntent.putExtras(bundle);
    HomeActivity.this.startActivity(myIntent);
}
XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android51.HomeActivity">

    <Switch
        android:id="@+id/toggle_record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:switchPadding="170dp"
        android:text="Record All Gameplay"
        android:checked="true"
        android:onClick="onSwitchButtonClicked"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="16dp" />


您可以发布activity_record.XML的XML代码吗?我想查看id为“toggle_record”的视图,但为什么在toRecord方法中找到它的id?创建此XML是否可被此活动访问时应执行此操作?@okset但开关仅存在于我的记录活动中,如果我在家中调用它,它将不知道开关是否正确?@Arunava哦,这是如何工作的!听起来这可能是我的问题
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android51.HomeActivity">

    <Switch
        android:id="@+id/toggle_record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:switchPadding="170dp"
        android:text="Record All Gameplay"
        android:checked="true"
        android:onClick="onSwitchButtonClicked"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="16dp" />