Android Studio открывает не приложение, а эмулятор

#android #kotlin

#Android #kotlin

Вопрос:

Я написал небольшое приложение в Android Studio с помощью kotline, и приложение не запускается в телефоне emulatet. Я активировал Logcat и получил несколько предупреждений или сбой. Он работал за 1 день до этого без проблем, я просто добавил метод addCart. Я программирую 3 месяца и знаю только основы, и с этой проблемой я больше не могу справиться, не задавая вопрос самостоятельно.

Я дам вам свой код и журналы. Мне не нужна помощь в коде, если он не вызывает проблемы. Со временем мне станет лучше 🙂

Вот код:

Главная:

 class MainActivity : AppCompatActivity() {

    private val tag ="Main_Activity"
    private val shoppingCard = mutableListOf<Item>()
    private var txtPrice = 0.0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val addButton  = findViewById<Button>(R.id.addButton)
        val showButton = findViewById<Button>(R.id.showButton)

        addButton.setOnClickListener { Log.d(tag, "addButton was pressed");addCart(shoppingCard)}
        showButton.setOnClickListener { Log.d(tag, "showButton was pressed");printList(shoppingCard)}
    }

    private fun addCart(shoppingCard: MutableList<Item>){
         val name = editProductText.toString()
         val pr = editPriceText.toString()
         val price      = pr.toDouble()



        val p = Item(name,price)
        shoppingCard.add(p)
        calcPrice(shoppingCard)
    }

    private fun printList(shoppingCard: MutableList<Item>)
    {
       for(Item in shoppingCard.indices)println("The name of the product is ${shoppingCard[Item].name} and the price is
${shoppingCard[Item].price}")
    }

    private fun calcPrice(shoppingCard: MutableList<Item>)
    {
        for(Item in shoppingCard.indices) txtPrice shoppingCard[Item].price
    }

    private fun removeItem(shoppingCard: MutableList<Item>, Item: Item)
    {
        shoppingCard.remove(Item)
        txtPrice - Item.price
    } 
}
  

Класс элемента:

 class Item(name: String,price: Doubleuble ) {

    val name = name
    val price = price 
}
  

Основная активность XML:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical"
    >


    <TextView
            android:text="Product Name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" android:id="@ id/Name"/>
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:text="Type Here"
            android:ems="10"
            android:id="@ id/editProductText"/>
    <TextView
            android:text="Price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" android:id="@ id/Price"/>
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@ id/editPriceText"/>
    <Button
            android:text="Add"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" android:id="@ id/addButton"/>
    <Button
            android:text="SHOW SHOPPING CART"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" android:id="@ id/showButton"/>
    <TextView
            android:layout_width="match_parent"
            android:layout_height="38dp" android:id="@ id/txtPrice"/> </LinearLayout>
  

Вот журналы:

 E/NetworkScheduler: Invalid component specified.

2019-04-24 16:28:26.944 1592-3964/? E/AudioFlinger: not enough memory for AudioTrack size=131296

2019-04-24 16:28:26.946 1592-3964/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
2019-04-24 16:28:26.969 1592-4736/? I/AudioFlinger: AudioFlinger's thread 0xe6d83340 tid=4736 ready to run
2019-04-24 16:28:26.984 3095-3206/? E/IAudioFlinger: createRecord returned error -12
2019-04-24 16:28:26.984 3095-3206/? E/AudioRecord: AudioFlinger could not create record track, status: -12
2019-04-24 16:28:26.987 3095-3206/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
2019-04-24 16:28:27.004 3095-3206/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
  

Комментарии:

1. Просто добавьте val к переменным в Item конструкторе класса и удалите поля. Не решает проблему, но делает класс менее уродливым

2. Эй, ты имеешь в виду вот так: элемент класса (имя значения: строка, цена значения: двойная)?

3. Да, именно так. Это также удаляет объявление переменных в теле