我已经用kotline在Android Studio中编写了一个小应用程序,但该应用程序无法在仿真手机中启动。我已激活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.