質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

2回答

2913閲覧

Switchボタンによる画面遷移でnullが起きる

Aies

総合スコア21

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

1クリップ

投稿2019/01/23 05:18

編集2019/01/24 00:37

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>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2019/01/23 06:23

InputIncome Activity のレイアウト xml を提示して頂けますか?
Aies

2019/01/24 00:37

文字制限ですべて入りきらなかったのでEditTextの項目を削りましたが、提示しました。よろしくお願いします。
jimbe

2019/01/24 02:50

InputIncome Activity の onCreate で setContentView していると思いますが, この InputIncome Layout を指定されていますでしょうか. たまに, 似た画面を作っている時等でコピペしてしまい setContentView が別の画面の場合がありますので.
guest

回答2

0

自己解決

Inputのソースをレイアウトもアクティビティもコピーしていたので、InputIncomeレイアウトの参照先が.Inputになっていたり、InputIncomeアクティビティのレイアウト参照先がInputのままになっており、そこを直したら表示されるようになりました。すみません。次からもっとよくコードを読み直してから質問します。

投稿2019/01/24 02:50

Aies

総合スコア21

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

XMLでは"IncomeSwitch"だけど、コードでは"InputSwitch"になっています。
"InputSwitch"というIdのViewは存在しないので、findViewByIdの返り値がnullになります。

投稿2019/01/24 01:17

swordone

総合スコア20651

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問