AndroidStudioで電卓アプリを開発しています。
AndroidStudioで電卓アプリを開発してみようと思い立って、経験や知識も何も無い状態でしたので、取り敢えずこのリンクを参考に作業を進め、ソースコードをMainActivityにコピー&ペーストして実行ボタンを押しビルドすると、以下のエラーメッセージが出てしまいました。
どうか、有識者の方ご教授頂けないでしょうか。
エラーメッセージ
Unresolved refernce: kotlinx :5 Unresolved refernce: num0 :17 Unresolved refernce: formula :18 Variable expected :18 Unresolved refernce: formula :18 Unresolved refernce: num1 :21 Unresolved refernce: formula :22 Variable expected :22 Unresolved refernce: formula :22 Unresolved refernce: num2 :25 Unresolved refernce: formula :26 Variable expected :26 Unresolved refernce: formula :26 Unresolved refernce: num3 :29 Unresolved refernce: formula :30 Variable expected :30 Unresolved refernce: formula :30 Unresolved refernce: num4 :33 Unresolved refernce: formula :34 Variable expected :34 Unresolved refernce: formula :34 Unresolved refernce: num5 :37 Unresolved refernce: formula :38 Variable expected :38 Unresolved refernce: formula :38 Unresolved refernce: num6 :41 Unresolved refernce: formula :42 Variable expected :42 Unresolved refernce: formula :42 Unresolved refernce: num7 :45 Unresolved refernce: formula :46 Variable expected :46 Unresolved refernce: formula :46 Unresolved refernce: num8 :49 Unresolved refernce: formula :50 Variable expected :50 Unresolved refernce: formula :50 Unresolved refernce: num9 :53 Unresolved refernce: formula :54 Variable expected :54 Unresolved refernce: formula :54 Unresolved refernce: point :57 Unresolved refernce: formula :58 Variable expected :58 Unresolved refernce: formula :58 Unresolved refernce: equal :61 Unresolved refernce: formula :62 Variable expected :62 Unresolved refernce: formula :62 Unresolved refernce: formula :65 Variable expected :65 Unresolved refernce: add :70 Unresolved refernce: formula :71 variable expected :71 Unresolved refernce: formula :71 Function invocation 'subtract(...)' expected :75 <html>Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:<br/>public infix fun <T> Array<out TypeVariable(T)>.subtract(other: Iterable<TypeVariable(T)>): Set<TypeVariable(T)> defined in kotlin.collections<br/>public infix fun BooleanArray.subtract(other: Iterable<Boolean>): Set<Boolean> defined in kotlin.collections<br/>public infix fun ByteArray.subtract(other: Iterable<Byte>): Set<Byte> defined in kotlin.collections<br/>public infix fun CharArray.subtract(other: Iterable<Char>): Set<Char> defined in kotlin.collections<br/>public infix fun DoubleArray.subtract(other: Iterable<Double>): Set<Double> defined in kotlin.collections<br/>public infix fun FloatArray.subtract(other: Iterable<Float>): Set<Float> defined in kotlin.collections<br/>public infix fun IntArray.subtract(other: Iterable<Int>): Set<Int> defined in kotlin.collections<br/>public infix fun LongArray.subtract(other: Iterable<Long>): Set<Long> defined in kotlin.collections<br/>public infix fun ShortArray.subtract(other: Iterable<Short>): Set<Short> defined in kotlin.collections<br/>public infix fun <T> Iterable<TypeVaria :75 Unresolved refernce: formula :76 Variable expected :76 Unresolved refernce: formula :76 Unresolved refernce: multiply :80 Unresolved refernce: formula :81 Variable expected :81 Unresolved refernce: formula :81 Unresolved refernce: devide :85 Unresolved refernce: formula :86 Variable expected :86 Unresolved refernce: formula :86 Unresolved refernce: delete :90 Unresolved refernce: formula :90 Unresolved refernce: formula :93 Variable expected :93 Unresolved refernce: percent :99 Unresolved refernce: formula :100 Variable expected :100 Unresolved refernce: formula :100 Unresolved refernce: sigh :102 Function invocation 'clear(...)' expected :105 <html>Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:<br/>public fun kotlin.text.StringBuilder /* = java.lang.StringBuilder */.clear(): kotlin.text.StringBuilder /* = java.lang.StringBuilder */ defined in kotlin.text :105 Unresolved refernce: formula :106 Variable expected :106 Unresolved refernce: formula :120 Variable expected :120 Unresolved refernce: formula :129 Variable expected :129
該当のソースコード
kotlin
1package com.example.dentaku1 2 3import androidx.appcompat.app.AppCompatActivity 4import android.os.Bundle 5import kotlinx.android.synthetic.main.activity_main.* 6 7class MainActivity : AppCompatActivity() { 8 9 var nStr : String = "" 10 val nList = ArrayList<Double>() // arraylist to store numbers 11 val oList = ArrayList<Char>() // arraylist to store operations 12 13 override fun onCreate(savedInstanceState: Bundle?) { 14 super.onCreate(savedInstanceState) 15 setContentView(R.layout.activity_main) 16 17 num0.setOnClickListener { 18 formula.text = "${formula.text}0" 19 nStr += "0" 20 } 21 num1.setOnClickListener { 22 formula.text = "${formula.text}1" 23 nStr += "1" 24 } 25 num2.setOnClickListener { 26 formula.text = "${formula.text}2" 27 nStr += "2" 28 } 29 num3.setOnClickListener { 30 formula.text = "${formula.text}3" nStr += "3" 3132 } 323 33 num4.setOnClickListener { 34 formula.text = "${formula.text}4" 35 nStr += "4" 36 } 37 num5.setOnClickListener { 38 formula.text = "${formula.text}5" 39 nStr += "5" 40 } 41 num6.setOnClickListener { 42 formula.text = "${formula.text}6" 43 nStr += "6" 44 } 45 num7.setOnClickListener { 46 formula.text = "${formula.text}7" 47 nStr += "7" 48 } 49 num8.setOnClickListener { 50 formula.text = "${formula.text}8" 51 nStr += "8" 52 } 53 num9.setOnClickListener { 54 formula.text = "${formula.text}9" 55 nStr += "9" 56 } 57 point.setOnClickListener { 58 formula.text = "${formula.text}." 59 nStr += "." 60 } 61 equal.setOnClickListener { 62 formula.text = "${formula.text}=" 63 addList(nStr) 64 var result = calcualte().toString() 65 formula.text = result 66 nStr = result 67 nList.clear() 68 oList.clear() 69 } 70 add.setOnClickListener { 71 formula.text = "${formula.text}+" 72 addList(nStr,'+') 73 nStr = "" 74 } 75 subtract.setOnClickListener { 76 formula.text = "${formula.text}-" 77 addList(nStr,'-') 78 nStr = "" 79 } 80 multiply.setOnClickListener { 81 formula.text = "${formula.text}*" 82 addList(nStr,'*') 83 nStr = "" 84 } 85 divide.setOnClickListener { 86 formula.text = "${formula.text}/" 87 addList(nStr,'/') 88 nStr = "" 89 } 90 delete.setOnClickListener { 91 var formulaStr = formula.text.toString() 92 if (!formulaStr.isEmpty()) { 93 formula.text = formulaStr.subSequence(0,formulaStr.lastIndex) 94 } 95 if (!nStr.isEmpty()) { 96 nStr = nStr.substring(0, nStr.lastIndex) 97 } 98 } 99 percent.setOnClickListener { 100 formula.text = "${formula.text}%" 101 } 102 sign.setOnClickListener { 103 104 } 105 clear.setOnClickListener { 106 formula.text = "" 107 nStr = "" 108 nList.clear() 109 oList.clear() 110 } 111 112 } // end fun onCreate 113 114 fun addList(str : String, ope : Char) { 115 try { 116 var num = str.toDouble() 117 nList.add(num) 118 oList.add(ope) 119 }catch(e:Exception){ 120 formula.text = "Numeric error" 121 } 122 } 123 124 fun addList(str : String) { 125 try { 126 var num = str.toDouble() 127 nList.add(num) 128 }catch(e:Exception){ 129 formula.text = "Numeric error" 130 } 131 } 132 133 fun calcualte() : Double { 134 135 var i = 0 136 while (i < oList.size) { 137 //do multiplication and division first 138 if(oList.get(i) == '*' || oList.get(i) == '/') { 139 var result = if (oList.get(i) == '*') nList.get(i) * 140nList.get(i+1) else nList.get(i) / nList.get(i+1) 141 nList.set(i,result) 142 nList.removeAt(i+1) 143 oList.removeAt(i) 144 i-- 145 } 146 // change subtraction to addition 147 else if(oList.get(i) == '-'){ 148 oList.set(i,'+') 149 nList.set(i+1,nList.get(i+1) * -1) 150 } 151 i++ 152 } 153 154 // get sum 155 var result = 0.0 156 for (i in nList){ 157 result += i 158 } 159 160 return result 161 }// end fun calcualte 162 163} // end class
試したこと
コピペですので誤字脱字は無いと思います。
fileやlayoutを確認し直してもう一度ビルドを実行してみましたが変化は見受けられませんでした。
補足情報(FW/ツールのバージョンなど)
先日ダウンロードしたばかりですのでAndroid Studioのバージョンは最新です。activity_mainは以下のようになっていますのでご指摘ありましたら宜しくお願い致します。