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

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

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

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

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

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

Q&A

解決済

2回答

3987閲覧

Unfortunately ファイル名 has stopped がでます。Androidアプリです。

edoooooo

総合スコア476

Java

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

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

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

0グッド

0クリップ

投稿2016/12/06 06:46

AnotherCalcActivityです。

java

1package com.example.android.sample.calculator; 2 3import android.content.Intent; 4import android.support.v7.app.AppCompatActivity; 5import android.os.Bundle; 6import android.text.Editable; 7import android.text.TextUtils; 8import android.text.TextWatcher; 9import android.view.View; 10import android.widget.EditText; 11import android.widget.Spinner; 12import android.widget.TextView; 13 14public class AnotherCalcActivity extends AppCompatActivity implements TextWatcher, View.OnClickListener{ 15 16 17 //上のEditText 18 private EditText numberInput1; 19 //下のEditText 20 private EditText numberInput2; 21 22 23 //演算子選択用のSpinner 24 private Spinner operatorSelector; 25 26 //計算結果表示用のTextView 27 private TextView calcResult; 28 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 34 35 //戻るボタン 36 findViewById(R.id.backButton).setOnClickListener(this); 37 38 39 //上のEditText 40 numberInput1=(EditText)findViewById(R.id.numberInput1); 41 //上のEditTextの文字入力イベントを受け取る 42 numberInput1.addTextChangedListener(this); 43 44 //下のEditText 45 numberInput2=(EditText)findViewById(R.id.numberInput2); 46 47 //下のEditTextの文字入力を受け取る 48 numberInput2.addTextChangedListener(this); 49 50 //演算子選択用のSpinner 51 operatorSelector=(Spinner)findViewById(R.id.operatorSelector); 52 53 //計算結果表示用のTextView 54 calcResult=(TextView)findViewById(R.id.calcResult); 55 56 } 57 58 59 @Override 60 public void onClick(View v) { 61 62 //戻るボタンが押された時 63 //どちらかのEditTextに値が入っていない場合 64 if (!checkEditTextInput()) { 65 66 //キャンセルとみなす 67 setResult(RESULT_CANCELED); 68 } else { 69 70 //計算結果 71 int result = calc(); 72 73 74 //インテントを生成し、計算結果を詰める 75 Intent data = new Intent(); 76 data.putExtra("result", result); 77 setResult(RESULT_OK, data); 78 } 79 //アクティビティを終了 80 finish(); 81 82 83 } 84 85 86 //2つのEditTextに入力がされているかをチェックする 87 private boolean checkEditTextInput(){ 88 //入力内容を取得する 89 String input1=numberInput1.getText().toString(); 90 String input2=numberInput2.getText().toString(); 91 92 //2つともから文字列(あるいはnull)でなければ、true 93 return !TextUtils.isEmpty(input1)&& !TextUtils.isEmpty(input2); 94 } 95 96 @Override 97 public void beforeTextChanged(CharSequence s,int start,int count,int after){ 98 //テキストが変更される直前に呼ばれる。Sは変更前の内容 99 100 } 101 102 @Override 103 public void onTextChanged(CharSequence s,int start,int before,int count){ 104 //テキストが変更される時に呼ばれる。sは変更後の内容で編集不可 105 } 106 107 @Override 108 public void afterTextChanged(Editable s){ 109 //テキストが変更された後に呼ばれる。sは変更後の内容で編集可能 110 //必要があれば、計算を行い、結果を表示する 111 refreshResult(); 112 113 } 114 115 //計算結果の表示を更新する 116 private void refreshResult(){ 117 if(checkEditTextInput()) { 118 //計算を行う 119 int result = calc(); 120 121 //計算結果用のTextVieを書き換える 122 String resultText = getString(R.string.calc_result_text, result); 123 calcResult.setText(resultText); 124 125 }else{ 126 //どちらかが入力されていない状態の場合、計算結果用の表示をデフォルトに戻す 127 calcResult.setText(R.string.calc_result_default); 128 } 129 } 130 131 132 //計算を行う 133 private int calc(){ 134 //入力内容を取得する 135 String input1=numberInput1.getText().toString(); 136 String input2=numberInput2.getText().toString(); 137 138 //int型に変換する 139 int number1=Integer.parseInt(input1); 140 int number2=Integer.parseInt(input2); 141 142 //Spinnerから、洗濯中のindexを取得する 143 int operator=operatorSelector.getSelectedItemPosition(); 144 145 //indexに応じて計算結果を返す 146 switch(operator){ 147 case 0://足し算 148 return number1+number2; 149 case 1://引き算 150 return number1-number2; 151 case 2://掛け算 152 return number1*number2; 153 case 3://割り算 154 return number1/number2; 155 default: 156 //通常発生しない 157 throw new RuntimeException(); 158 } 159 } 160 161 162 163 164}

ログのエラーです。

java

1FATAL EXCEPTION: main 212-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: Process: com.example.android.sample.calculator, PID: 1740 312-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.sample.calculator/com.example.android.sample.calculator.AnotherCalcActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 412-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 512-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 612-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java) 712-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 812-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 912-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148) 1012-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417) 1112-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 1212-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 1312-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 1412-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 1512-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at com.example.android.sample.calculator.AnotherCalcActivity.onCreate(AnotherCalcActivity.java:36) 1612-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6237) 1712-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 1812-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 1912-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  2012-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)  2112-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  2212-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)  2312-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)  2412-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)  2512-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)  2612-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  2712-06 15:32:13.454 1740-1740/com.example.android.sample.calculator E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

なぜnullpointerがでてしまうのでしょうか?よろしくお願いします。

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

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

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

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

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

guest

回答2

0

ベストアンサー

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

これは、nullオブジェクトに対してsetOnClickListenerを呼び出そうとしたことを意味しています。
つまり、36行目でsetOnClickListenerしようとしたfindViewById(R.id.backButton)がnullではないか?ということです。
これがnullになるのは、引数となるidを持つViewが存在しない場合です。xmlで設定しているidを確認してください。

投稿2016/12/07 14:39

swordone

総合スコア20651

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

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

edoooooo

2016/12/08 13:22 編集

回答をいただきましてありがとうございます。 activity_another_calc.xmlに idがbackButtonのものを設置しています。 Buttonがnullではないか? ということは、buckButtonが設置されているか、いないかという考えは的はずれでしょうか?申し訳ありません。どうぞよろしくお願いします。
swordone

2016/12/08 13:28

いまこのActivityにセットしているのはactivity_mainです。この中に対象のViewが存在するかの問題です。
edoooooo

2016/12/18 02:36

一から作り直したところ、 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_another_calc); この最後の行のsetしているViewが R.layout.activity_main); と間違えていたためエラーが起こっていました。ありがとうございました。
guest

0

ログの2行目の後半には、以下のように書かれています。
'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

ここで使われているreferenceは、android.view.View$OnClickListener しかありませんので、これが無効な参照(null object reference)なのだと考えられます。

setOnClickListnerの引数には、clickされた時に呼び出されるメソッドを指定します。
質問に書かれたコードで、それらしい名前を持つのは、AnotherCalcActivity.onClick です。

この辺りが、問題解決のヒントになるのではないかと思います。

投稿2016/12/06 08:40

coco_bauer

総合スコア6915

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

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

edoooooo

2016/12/06 10:30

ありがとうございます。 無効となっている、AnotherCalcActivity.onClickの onClick()では、 @Override public void onClick(View v) { //戻るボタンが押された時 //どちらかのEditTextに値が入っていない場合 if (!checkEditTextInput()) { //キャンセルとみなす setResult(RESULT_CANCELED); } else { //計算結果 int result = calc(); //インテントを生成し、計算結果を詰める Intent data = new Intent(); data.putExtra("result", result); setResult(RESULT_OK, data); } このような処理をしていて、しっかりとputExtraや、setResultされていてnullPointになることは、ないかと思うのですが、教えていただけないでしょうか?
coco_bauer

2016/12/07 02:13

「android.view.View$OnClickListener」がnullなのではないかと私は疑っています。 AnotherCalcActivityクラスのコードに問題があるかどうかは検討していませんが、「android.view.View$OnClickListener」がnullになることに関係していると思える箇所がありません。 コードの中でsetOnClickListenerが使われている箇所を順に調べて行って、その引数に誤りがないか確認してみてください。
swordone

2016/12/08 06:20

リスナーがnullの場合、そのViewをクリックしても何も起きなくなるだけで、例外にはなりません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問