S
S
solarLi2020-10-14 22:13:01
Android
solarLi, 2020-10-14 22:13:01

What could be the reason for the Android application not launching (see the code in details)?

There is a code in kotlin. There are no error messages, but the application does not start. What could be the problem?

import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {

    // получаем элемент ListView
    var plantsList: ListView? = null

    // получаем ресурсы
    //растения
    var plants = resources.getStringArray(R.array.plants)
    //список для удаления
    var selectedPlants = resources.getStringArray(R.array.selectedPlants)

    // создаем адаптер
    var adapter: ArrayAdapter<String>? = null

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

        plantsList = findViewById(R.id.plantsList)
        adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, plants)

        // устанавливаем для списка адаптер
        plantsList!!.adapter = adapter

        // добвляем для списка слушатель
        plantsList!!.onItemClickListener = AdapterView.OnItemClickListener { parent, v, position, id ->
            // получаем нажатый элемент
            var  phone = adapter!!.getItem(position);
            if(plantsList!!.isItemChecked(position)==true){
                add(selectedPlants)
            }
            else{

                remove(selectedPlants)
            }

        }
    }

    fun add(view: Array<String>) {
        val plantEditText = findViewById<EditText>(R.id.plant)
        val plant = plantEditText.text.toString()
        if (!plant.isEmpty() && plants.contains(plant) == false) {
            adapter!!.add(plant)
            plantEditText.setText("")
            adapter!!.notifyDataSetChanged()
        }
    }


    fun remove(view: Array<String>) {
        // получаем и удаляем выделенные элементы
        for (i in selectedPlants.indices) {
            adapter!!.remove(selectedPlants.get(i))
        }
        // снимаем все ранее установленные отметки
        plantsList!!.clearChoices()
        // очищаем массив выбраных объектов
        selectedPlants.isEmpty()
        adapter!!.notifyDataSetChanged()
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Vodakov, 2020-10-15
@WaterSmith

For example, in the manifest, either there are not enough resources, or there is a typo somewhere.
Where are there no mistakes? On the device or in the logs?
They should not be on the device.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question