Android应用无法在任何模拟器中运行

时间:2017-07-16 12:21:30

标签: android android-studio kotlin

我的应用程序无法运行。可能有一些我无法弄清楚的错误。

那么有人能告诉我为什么并帮我解决代码吗?

这是我的代码:

package com.djrockerstudio.pc

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

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

        btnCal.setOnClickListener {
            val sum: Int = cal()
            result.text = String.format("$%,.2f", sum)
        }

    }
    private fun cal(): Int {
        val pStandardIndex = 1000
        val pIndex1 = 1500
        val pIndex2 = 2000
        val pIndexOver2 = 2500
        val powerIndex: Int = inp.text.toString().toInt()
        val standIndex: Int = inp2.text.toString().toInt()
        val indexL1: Int = inp3.text.toString().toInt()
        val indexL2: Int = inp4.text.toString().toInt()
        var std = 0
        var idx1 = 0
        var idx2 = 0
        var oIdx2 = 0
        when (powerIndex) {
            in 0..standIndex            -> std      = standIndex*pStandardIndex
            in (standIndex+1)..indexL1  -> idx1     = (indexL1-standIndex)*pIndex1
            in (indexL1+1)..indexL2     -> idx2     = (indexL2-indexL1)*pIndex2
            else                        -> oIdx2    = (powerIndex-indexL2)*pIndexOver2
        }
        val sum = std+idx1+idx2+oIdx2
        return sum
    }

}

这是我的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.djrockerstudio.pc.MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:orientation="vertical"
        android:padding="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Power index (Kwh)"
                    android:textSize="14sp"
                    android:textColor="#000"
                    android:padding="5dp"
                    android:layout_margin="6dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Standard index"
                    android:textSize="14sp"
                    android:textColor="#000"
                    android:padding="5dp"
                    android:layout_margin="6dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Index level 1"
                    android:textSize="14sp"
                    android:textColor="#000"
                    android:padding="5dp"
                    android:layout_margin="6dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Index level 2"
                    android:textSize="14sp"
                    android:textColor="#000"
                    android:padding="5dp"
                    android:layout_margin="6dp"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <EditText
                    android:id="@+id/inp"
                    android:background="@drawable/edit_text_style"
                    android:layout_width="200dp"
                    android:layout_height="30dp"
                    android:layout_margin="5dp"
                    android:inputType="numberDecimal"/>
                <EditText
                    android:id="@+id/inp2"
                    android:background="@drawable/edit_text_style"
                    android:layout_width="200dp"
                    android:layout_height="30dp"
                    android:layout_margin="5dp"
                    android:inputType="numberDecimal"/>
                <EditText
                    android:id="@+id/inp3"
                    android:background="@drawable/edit_text_style"
                    android:layout_width="200dp"
                    android:layout_height="30dp"
                    android:layout_margin="5dp"
                    android:inputType="numberDecimal"/>
                <EditText
                    android:id="@+id/inp4"
                    android:background="@drawable/edit_text_style"
                    android:layout_width="200dp"
                    android:layout_height="30dp"
                    android:layout_margin="5dp"
                    android:inputType="numberDecimal"/>
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center"
            android:padding="10dp">
            <Button
                android:id="@+id/btnCal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Calculate"
                android:background="@drawable/btn_style"
                android:textColor="#FFF" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Total amount:"
                android:textSize="14sp"
                android:textColor="#000"
                android:padding="5dp"/>
            <TextView
                android:id="@+id/result"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:textColor="#00F"
                android:padding="5dp"/>
        </LinearLayout>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

应用程序无法解决的问题是因为我使用Bluestack并且它无法正常工作。我发现上面的代码工作的解决方案就在这里:

private fun cal(): Double {
        val pStandardIndex = 1000
        val pIndex1 = 1500
        val pIndex2 = 2000
        val pIndexOver2 = 2500
        val powerIndex = inp.text.toString().toInt()
        val standIndex = inp2.text.toString().toInt()
        val indexL1 = inp3.text.toString().toInt()
        val indexL2 = inp4.text.toString().toInt()
        val sum: Double
        when (powerIndex) {
            in 0 .. standIndex -> sum = standIndex*pStandardIndex.toDouble()
            in (standIndex+1) .. indexL1 -> sum = (standIndex*pStandardIndex) + ((indexL1-standIndex)*pIndex1).toDouble()
            in (indexL1+1) .. indexL2 -> sum = (standIndex*pStandardIndex) + ((indexL1-standIndex)*pIndex1) + ((indexL2-indexL1)*pIndex2).toDouble()
            else -> sum = (standIndex*pStandardIndex) + ((indexL1-standIndex)*pIndex1) + ((indexL2-indexL1)*pIndex2) + ((powerIndex-indexL2)*pIndexOver2).toDouble()
        }
        return sum
    }