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がでてしまうのでしょうか?よろしくお願いします。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/08 13:22 編集
2016/12/08 13:28
2016/12/18 02:36