switchボタンで画面遷移をするようにしたいのですが、InputActivityからInputIncomeに遷移しようとすると一番下にあるエラー文が表示されます。(エラーの個所はswitchButton.setOnCheckedChangeListenerの行です。)
()の中のどこかがnullでエラーが起きていると思ったのですが、全くわからなかったのでよろしければご助言お願いいたします。
下の二つのソースはonCreateの中の一番下に作ってあります
Java
1Input Activity 2 3// idがswitchButtonのSwitchを取得 4 Switch switchButton = (Switch) findViewById(R.id.InputSwitch); 5 // switchButtonのオンオフが切り替わった時の処理を設定 6 switchButton.setOnCheckedChangeListener( 7 new CompoundButton.OnCheckedChangeListener(){ 8 public void onCheckedChanged(CompoundButton comButton, boolean isChecked){ 9 // 表示する文字列をスイッチのオンオフで変える 10 // オンなら 11 if(isChecked){ 12 Intent in = new Intent(getApplication(), InputIncome.class); 13 startActivity(in); 14 } 15 // オフなら 16 } 17 } 18 ); 19 }
Java
1InputIncome Activity 2 3 // idがswitchButtonのSwitchを取得 4 Switch switchButton = (Switch) findViewById(R.id.IncomeSwitch); 5 // switchButtonのオンオフが切り替わった時の処理を設定 6 switchButton.setOnCheckedChangeListener( 7 new CompoundButton.OnCheckedChangeListener(){ 8 public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked){ 9 // 表示する文字列をスイッチのオンオフで変える 10 // オンなら 11 if(isChecked){ 12 Intent in = new Intent(getApplication(), Input.class); 13 startActivity(in); 14 } 15 // オフなら 16 } 17 } 18 );
error
1Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference 2 at com.example.a162105.kakeibo.InputIncome.onCreate(InputIncome.java:128)
Java
1InputIncome Layout 2<?xml version="1.0" encoding="utf-8"?> 3<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:id="@+id/container" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 tools:context=".Input"> 10 11 12 <android.support.design.widget.BottomNavigationView 13 android:id="@+id/navigation" 14 android:layout_width="0dp" 15 android:layout_height="wrap_content" 16 android:layout_marginStart="0dp" 17 android:layout_marginEnd="0dp" 18 android:background="?android:attr/windowBackground" 19 app:layout_constraintBottom_toBottomOf="parent" 20 app:layout_constraintLeft_toLeftOf="parent" 21 app:layout_constraintRight_toRightOf="parent" 22 app:menu="@menu/navigation" /> 23 24 <Button 25 android:id="@+id/addbutton" 26 style="@style/Widget.AppCompat.Button.Colored" 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:layout_marginStart="8dp" 30 android:layout_marginLeft="8dp" 31 android:layout_marginTop="8dp" 32 android:layout_marginEnd="8dp" 33 android:layout_marginRight="8dp" 34 android:layout_marginBottom="8dp" 35 android:text="追加" 36 android:onClick="onClickAddButton" 37 app:layout_constraintBottom_toBottomOf="parent" 38 app:layout_constraintEnd_toEndOf="parent" 39 app:layout_constraintHorizontal_bias="0.971" 40 app:layout_constraintStart_toStartOf="parent" 41 app:layout_constraintTop_toTopOf="parent" 42 app:layout_constraintVertical_bias="0.879" /> 43 44 <TextView 45 android:id="@+id/textView" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content" 48 android:layout_marginStart="8dp" 49 android:layout_marginLeft="8dp" 50 android:layout_marginTop="16dp" 51 android:layout_marginEnd="8dp" 52 android:layout_marginRight="8dp" 53 android:layout_marginBottom="8dp" 54 android:text="金額" 55 app:layout_constraintBottom_toBottomOf="parent" 56 app:layout_constraintEnd_toEndOf="parent" 57 app:layout_constraintHorizontal_bias="0.0" 58 app:layout_constraintStart_toStartOf="parent" 59 app:layout_constraintTop_toTopOf="parent" 60 app:layout_constraintVertical_bias="0.0" /> 61 62 <TextView 63 android:id="@+id/textView2" 64 android:layout_width="75dp" 65 android:layout_height="22dp" 66 android:layout_marginStart="8dp" 67 android:layout_marginLeft="8dp" 68 android:layout_marginTop="8dp" 69 android:layout_marginEnd="8dp" 70 android:layout_marginRight="8dp" 71 android:layout_marginBottom="8dp" 72 android:text="カテゴリー" 73 app:layout_constraintBottom_toBottomOf="parent" 74 app:layout_constraintEnd_toEndOf="parent" 75 app:layout_constraintHorizontal_bias="0.0" 76 app:layout_constraintStart_toStartOf="parent" 77 app:layout_constraintTop_toBottomOf="@+id/textView" 78 app:layout_constraintVertical_bias="0.307" /> 79 80 <TextView 81 android:id="@+id/textView3" 82 android:layout_width="wrap_content" 83 android:layout_height="wrap_content" 84 android:layout_marginStart="8dp" 85 android:layout_marginLeft="8dp" 86 android:layout_marginTop="44dp" 87 android:layout_marginEnd="8dp" 88 android:layout_marginRight="8dp" 89 android:layout_marginBottom="8dp" 90 android:text="メモ" 91 app:layout_constraintBottom_toBottomOf="parent" 92 app:layout_constraintEnd_toEndOf="parent" 93 app:layout_constraintHorizontal_bias="0.023" 94 app:layout_constraintStart_toStartOf="parent" 95 app:layout_constraintTop_toBottomOf="@+id/textView2" 96 app:layout_constraintVertical_bias="0.008" /> 97 98 <TextView 99 android:id="@+id/textView4" 100 android:layout_width="wrap_content" 101 android:layout_height="wrap_content" 102 android:layout_marginStart="8dp" 103 android:layout_marginLeft="8dp" 104 android:layout_marginTop="48dp" 105 android:layout_marginEnd="8dp" 106 android:layout_marginRight="8dp" 107 android:layout_marginBottom="8dp" 108 android:text="日付" 109 app:layout_constraintBottom_toBottomOf="parent" 110 app:layout_constraintEnd_toEndOf="parent" 111 app:layout_constraintHorizontal_bias="0.023" 112 app:layout_constraintStart_toStartOf="parent" 113 app:layout_constraintTop_toBottomOf="@+id/textView3" 114 app:layout_constraintVertical_bias="0.023" /> 115 116 <Switch 117 android:id="@+id/IncomeSwitch" 118 android:layout_width="wrap_content" 119 android:layout_height="30dp" 120 android:layout_marginStart="8dp" 121 android:layout_marginLeft="8dp" 122 android:layout_marginTop="8dp" 123 android:layout_marginEnd="8dp" 124 android:layout_marginRight="8dp" 125 android:layout_marginBottom="8dp" 126 android:switchTextAppearance="@style/TextAppearance.AppCompat.Caption" 127 android:text="Switch" 128 app:layout_constraintBottom_toBottomOf="@+id/navigation" 129 app:layout_constraintEnd_toEndOf="parent" 130 app:layout_constraintHorizontal_bias="0.539" 131 app:layout_constraintStart_toStartOf="parent" 132 app:layout_constraintTop_toTopOf="parent" 133 app:layout_constraintVertical_bias="0.0" /> 134 135</android.support.constraint.ConstraintLayout>
回答2件
あなたの回答
tips
プレビュー