2017-05-30 58 views
-1

我剛剛進入kotlin昨天,我嘗試使用Android studio 3.0 Canary 2,JRE 1.8.0創建一個基本計算器。不幸的是,當我應用程序安裝到我的手機,原來不同的像下面佈局問題和使用kotlin開發的android應用程序中的錯誤

Kotlin calculator in my phone

形象在我的Android工作室,設計是這樣的

kotlin calculator in android studio

這裏是我的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> 

雖然還有第二個問題,但是

我試圖在數字字段中輸入數字,當我點擊+(添加)按鈕時立即崩潰。

這裏的錯誤

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) 

這顯然表明,INT是一個錯誤。

這是我的MainActivity代碼。

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()); 
    } 
} 

事實證明,當我parseInt從手機檢索的字符串是一個錯誤。請幫助:)

+0

Kotlin不是問題。學習如何使用ConstraintLayout以及在嘗試解析數字之前如何處理空輸入字符串 –

+0

感謝幫助:)它解決了我的問題,我也是android studio中的新成員。只是意識到constraintLayout對於控制佈局非常重要。 –

回答

相關問題