-1

I just got into kotlin yesterday, and I attempted to create a basic calculator using Android studio 3.0 Canary 2,JRE 1.8.0. Unfortunately, when I install the application into my phone, it turns out differently like the image below

Kotlin calculator in my phone

In my android studio, the design was like this

kotlin calculator in android studio

Here is my xml

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 
    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"
    tools:context="com.example.jeffrey.kotlincalculator.MainActivity">

    <EditText
        android:id="@+id/editText_num1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:hint="Input Number 1"
        tools:layout_editor_absoluteX="40dp"
        tools:layout_editor_absoluteY="27dp" />

    <EditText
        android:id="@+id/editText_num2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:hint="Input Number 2"
        tools:layout_editor_absoluteX="40dp"
        tools:layout_editor_absoluteY="92dp" />

    <TextView
        android:id="@+id/textview_result"
        android:layout_width="345dp"
        android:layout_height="39dp"
        android:text="Result"
        android:textAppearance="@android:style/TextAppearance"
        android:textAlignment="center"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteY="159dp"
        tools:layout_editor_absoluteX="20dp" />

    <Button
        android:id="@+id/button_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="48dp"
        android:text="+"
        app:layout_constraintLeft_toLeftOf="parent"
        tools:layout_editor_absoluteY="219dp" />

    <Button
        android:id="@+id/button_minus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        tools:layout_editor_absoluteY="291dp"
        tools:layout_editor_absoluteX="48dp" />

    <Button
        android:id="@+id/button_multiple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="*"
        tools:layout_editor_absoluteX="266dp"
        tools:layout_editor_absoluteY="219dp" />

    <Button
        android:id="@+id/button_divide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="/"
        tools:layout_editor_absoluteX="266dp"
        tools:layout_editor_absoluteY="291dp" />
</android.support.constraint.ConstraintLayout>

While there are second issue,

I tried to enter numbers into number field, when i click +(add)button it crashes immediately.

Here's the errors

FATAL EXCEPTION: main
  Process: com.example.jeffrey.kotlincalculator, PID: 20432
  java.lang.NumberFormatException: Invalid int: ""
      at java.lang.Integer.invalidInt(Integer.java:138)
      at java.lang.Integer.parseInt(Integer.java:358)
      at java.lang.Integer.parseInt(Integer.java:334)
      at com.example.jeffrey.kotlincalculator.MainActivity.getNum1(MainActivity.kt:42)
      at com.example.jeffrey.kotlincalculator.MainActivity$onCreate$1.onClick(MainActivity.kt:24)
      at android.view.View.performClick(View.java:5207)
      at android.view.View$PerformClick.run(View.java:21177)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5438)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)

it obviously shows that the INT was an error.

Here's my MainActivity code.

package com.example.jeffrey.kotlincalculator

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

        val result = findViewById(R.id.textview_result) as TextView
        val button_add = findViewById(R.id.button_add) as Button
        val button_minus = findViewById(R.id.button_minus) as Button
        val button_multiple = findViewById(R.id.button_multiple) as Button
        val button_divide = findViewById(R.id.button_divide) as Button

        button_add.setOnClickListener(View.OnClickListener{
            view -> result.text = (getNum1() + getNum2()).toString()
        })

        button_minus.setOnClickListener(View.OnClickListener{
            view -> result.text = (getNum1() - getNum2()).toString()
        })

        button_multiple.setOnClickListener(View.OnClickListener{
            view -> result.text = (getNum1() * getNum2()).toString()
        })

        button_divide.setOnClickListener(View.OnClickListener{
            view -> result.text = (getNum1() / getNum2()).toString()
        })
    }

    fun getNum1(): Int{
        val input_num1 = findViewById(R.id.editText_num1) as EditText;
        return Integer.parseInt(input_num1.text.toString());
    }

    fun getNum2(): Int{
        val input_num2 = findViewById(R.id.editText_num2) as EditText;
        return Integer.parseInt(input_num2.text.toString());
    }
}

it turns out that when i parseInt of the String that retrieve from the phone was an error. Please help:)

Jeffrey Cheong
  • 348
  • 3
  • 13
  • Kotlin is not the problem. Learn how to use ConstraintLayout and how to handle empty input strings before you attempt to parse a number – OneCricketeer May 30 '17 at 01:20
  • Thank for helping :) it solved my problem, i am new in android studio as well. just realized that constraintLayout are important to control the layout. – Jeffrey Cheong May 30 '17 at 02:49

1 Answers1

2

your xml has tools:layout_editor_absoluteX="40dp" tools:layout_editor_absoluteY="92dp" elements which means these constraints will be applied for the designer window only. Here's the training that might help you to understand how to use Constraint layout: https://developer.android.com/training/constraint-layout/index.html

Sputnik
  • 328
  • 1
  • 9