LoginSignup
ankomochi555
@ankomochi555

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

4つのうち1つボタンを押すと画面が遷移し遷移先で計算結果をTextViewで表示したい

Q&AClosed

解決したいこと

現在、小数点対応の計算アプリをkotlinで作成しており、遷移先で計算結果をTextViewで表示したいと考えております。
4つボタンがありボタンを押すと画面が遷移し遷移先で計算結果をTextViewで表示したいと考えております。
押したボタンに応じて四則計算させたいところ、遷移先でif文を使用し計算させようと思っているのですが、条件式を上手くたてられず計算結果をそれぞれ表示することができません。

拙い説明で申し訳ありませんが、
解決方法等ご教授いただけると幸いです

発生している問題・エラー

数値をいれ、ボタンを押すとactivity_second.xmlでのTextが表示されてしまいます。
kotlin初心者で関数については勉強中なのですが、おそらく関数をうまく使いこなせていないのが原因だと思っております。

MainActivity.kt 遷移元


import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() ,View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button1.setOnClickListener(this)
        button2.setOnClickListener(this)
        button3.setOnClickListener(this)
        button4.setOnClickListener(this)

    }

    //onClickメソッド内でSecondActivityに遷移させるよう記術
    override fun onClick(p0: View?) {

        /*
        val editText1:EditText = findViewById(R.id.editText1)
        val editText2:EditText = findViewById(R.id.editText2)
         */

        //数値に変換
        val x : Double = editText1.text.toString().toDouble()
        val y : Double = editText2.text.toString().toDouble()


        if (editText1.text.toString() == "" || editText2.text.toString() == "") {
            val rootLayout: View = findViewById(android.R.id.content)
            val snackbar = Snackbar.make(rootLayout, "数字をいれてください", Snackbar.LENGTH_LONG)
            snackbar.show()

        } else {
            val intent = Intent(this, SecondActivity::class.java)

            intent.putExtra("VALUE1", x)
            intent.putExtra("VALUE2", y)

            startActivity(intent)
        }
    }

activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText1"
        android:inputType="numberDecimal"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:inputType="numberDecimal"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="+"
        android:id="@+id/button1"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ー"
        android:id="@+id/button2"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="×"
        android:id="@+id/button3"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="÷"
        android:id="@+id/button4"/>


</LinearLayout>

SecondActivity.kt遷移先


import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_second.*

class SecondActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        val value1 = intent.getDoubleExtra("VALUE1", 0.0)
        val value2 = intent.getDoubleExtra("VALUE2", 0.0)


        fun  showAnswer (ans: View) {

            if (ans.id == R.id.button1) {
                Answer.text = "${value1 + value2}"
            }
            if (ans.id == R.id.button2) {
                Answer.text = "${value1 - value2}"
            }
            if (ans.id == R.id.button3)  {
                Answer.text = "${value1 * value2}"
            }
            if (ans.id == R.id.button4) {
                Answer.text = "${value1 / value2}"
            }
        }

       // Answer.text = "${value1 + value2}"
    }
}


activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SecondActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SecondActivity Answer"
        android:id="@+id/Answer"/>

</LinearLayout>

自分で試したこと

SecondActivity.ktのif文の条件式を調べ、変えてみましたが以下のようなエラーが出てしまいました。

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_second.*

class SecondActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        val value1 = intent.getDoubleExtra("VALUE1", 0.0)
        val value2 = intent.getDoubleExtra("VALUE2", 0.0)

        //  button1.equals(R.id.button1) ←ここでエラー


            if (button1.equals(R.id.button1)) {
                Answer.text = "${value1 + value2}"
            }
            if  (button2.equals(R.id.button2)) {
                Answer.text = "${value1 - value2}"
            }
            if  (button3.equals(R.id.button3))  {
                Answer.text = "${value1 * value2}"
            }
            if  (button4.equals(R.id.button4)) {
                Answer.text = "${value1 / value2}"
            }

       // Answer.text = "${value1 + value2}"
    }
}

エラーメッセージ

2022-04-11 12:40:20.433 6332-6332/jp.techacademy.arisa.takeishi.calapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: jp.example.calapp, PID: 6332
    java.lang.RuntimeException: Unable to start activity ComponentInfo{jp.example.calapp/jp.example.calapp.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
        at jp.example.calapp.SecondActivity.onCreate(SecondActivity.kt:21)
        at android.app.Activity.performCreate(Activity.java:6662)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
0

1Answer

次の画面へ行くときにどのボタンを押したのかをも渡す必要がありそうですね。

1

Comments

  1. @ankomochi555

    Questioner
    どのボタンを押したかを渡すことができ、無事計算できるようになりました!
    複数ボタンがある際はそのデータも送る必要があると学びました。
    ご回答ありがとうございました!

Your answer might help someone💌