问题描述
在活动 A 中,一旦单击按钮,活动 B 就会针对结果 (startActivityForResult) 启动。在活动 B 中,用户填写信息并单击按钮。单击此按钮后,将添加一些额外信息并调用 setResult,传递 RESULT_OK 和意图。
我遇到的问题是将 Edittext 添加到 Intent 会导致一条错误消息,即没有 Activity 来处理文本。谁能明白为什么添加 Topping 1 会导致此错误。
这是错误信息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pos,PID: 28299
android.content.ActivityNotFoundException: No Activity found to handle Intent { (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1937)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1616)
at android.app.Activity.startActivityForResult(Activity.java:4487)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:675)
at android.app.Activity.startActivityForResult(Activity.java:4445)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:662)
at android.app.Activity.startActivity(Activity.java:4806)
at android.app.Activity.startActivity(Activity.java:4774)
at com.example.pos.PizzaActivity$onCreate$5.onClick(PizzaActivity.kt:111)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
活动 A:OrderActivity.kt
package com.example.pos
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.multiplerecyclerview.OrderAdapter
import kotlinx.android.synthetic.main.activity_order.*
import kotlinx.android.synthetic.main.activity_order.view.*
import kotlin.properties.Delegates
const val INDEX = 0
const val FILE_ID = 1
class OrderActivity : AppCompatActivity() {
val list = ArrayList<DataModel>() /*ArrayList that is type Data Model. */
/* Adapter class is initialized and list is passed in the param. */
val orderAdapter = OrderAdapter(this,getItemsList())
var total by Delegates.notNull<Int>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_order)
customerButton.setOnClickListener { /* Customer onclick button: Gets taken to the customer screen. */
val intent = Intent(this@OrderActivity,CustomerActivity::class.java) /* Creating an Intent to go to Customer Activity. */
startActivity(intent) /* Starting Activity. */
}
deliveryChargeButton.setOnClickListener { /* Customer onclick button: Gets taken to the customer screen. */
val intent = Intent(this@OrderActivity,DeliveryActivity::class.java) /* Creating an Intent to go to Customer Activity. */
startActivity(intent) /* Starting Activity. */
}
cancelOrderButton.setOnClickListener {/* cancelOrder onclick button: Return to the Dashboard,finish the activity.. */
val intent = Intent(this@OrderActivity,Dashboard::class.java) /* Creating an Intent to go to Customer Activity. */
startActivity(intent) /* Starting Activity. */
finish() /* Finishing the Activity. */
}
/* Set the LayoutManager that this RecyclerView will use. */
orderRecyclerView.layoutManager = LinearLayoutManager(this)
/* Adapter instance is set to the recyclerview to inflate the items. */
orderRecyclerView.adapter = orderAdapter
}
/* This function is invoked once we return from Pizza Activity. */
override fun onActivityResult(requestCode: Int,resultCode: Int,data: Intent?) {
super.onActivityResult(requestCode,resultCode,data)
Log.i("Order","OnActivityResult.\n") /* Adding Logging information. */
if(resultCode == RESULT_OK) { /* If the result is -1 "Okay". */
/* Calling the insertItem function to add a View to the RecyclerView. */
val itemName:String = data?.getStringExtra("itemName").toString() /* storing the values from arguments returned. */
val itemPrice:String = data?.getStringExtra("itemPrice").toString() /* storing the values from arguments returned. */
val itemSize:String = data?.getStringExtra("itemSize").toString() /* storing the values from arguments returned. */
val topping1:String = data?.getStringExtra("topping1").toString() /* storing the values from arguments returned. */
var temp = textViewPrice.text.toString().toDouble()
temp += itemPrice.toDouble()
temp = String.format("%.2f",temp).toDouble()
textViewPrice.text = temp.toString()
/* Calling the insertItem function to add a View to the RecyclerView. */
insertItem4(name = itemName,price = itemPrice,size = itemSize,topping1 = topping1)
} else {
Log.i("Order","onActivityResult failed\n") /* Logging the failed intent retreat. */
}
}
private fun insertItem4(name: String,price: String,size: String,topping1: String) {
Toast.makeText(this,"Item added!",Toast.LENGTH_SHORT).show() /* Toast Message to confirm insertion. */
val newItem = DataModel("$name","$size","$price","$topping1","",viewType = OrderAdapter.TOPPINGS_4) /* Adding the item with correct arguments */
list.add(INDEX,newItem) /* Adding Item at Position Index. */
orderAdapter.notifyItemInserted(INDEX) /* Notifying the Adapter of the addition. */
}
/* This function is invoked to insert Items into the RecyclerView. */
fun insertItem(name: String,size: String) {
Toast.makeText(this,viewType = OrderAdapter.NO_TOPPING) /* Adding the item with correct arguments */
list.add(INDEX,newItem) /* Adding Item at Position Index. */
orderAdapter.notifyItemInserted(INDEX) /* Notifying the Adapter of the addition. */
}
/* This function is invoked to insert Items with Extras into the RecyclerView. */
fun insertItemExtras(name: String,top1: String,top2: String,top3: String,top4: String) {
Toast.makeText(this,"$top1","$top2","$top3","$top4",newItem) /* Adding Item at Position Index. */
orderAdapter.notifyItemInserted(INDEX) /* Notifying the Adapter of the addition. */
}
private fun getItemsList(): ArrayList<DataModel> {
//list.add(DataModel("Romana","1","12.50","Pepperoni","Aubergine","Ex Mozz.","Salami",OrderAdapter.TOPPINGS_4))
//list.add(DataModel("American",viewType = OrderAdapter.NO_TOPPING))
return list
}
fun pizzaButton(view: View) {
val buttonView : Button = view as Button
val texty = buttonView.text.toString()
val price = buttonView.tag.toString()
Toast.makeText(this,"$texty clicked",Toast.LENGTH_SHORT).show()
var intent = Intent(this@OrderActivity,PizzaActivity::class.java) /* Creating an Intent to go to Pizza Activity. */
intent.putExtra("itemName","$texty") /* Adding the foodName to the intent. */
intent.putExtra("itemPrice","$price") /* Adding the foodPrice to the intent. */
startActivityForResult(intent,1) /* Starting Activity for result. */
}
}
活动 B:PizzaActivity.kt
package com.example.pos
import android.app.Activity
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import com.example.multiplerecyclerview.OrderAdapter
import kotlinx.android.synthetic.main.activity_order.*
import kotlinx.android.synthetic.main.activity_pizza.*
const val SIZE_SMALL = 10
const val SIZE_LARGE = 12
const val ADD_TOP_SMALL = .90
const val ADD_TOP_LARGE = 1.10
const val ADD_PREM_SMALL = 1.3
const val ADD_PREM_LARGE = 1.5
class PizzaActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_pizza) /* Loading this layout if the activity is a pizza. */
val pizzaName: TextView = findViewById(R.id.pizzaName)
pizzaName.text = intent.getStringExtra("itemName").toString() /* Adding the name of the pizza to Screen. */
val pizzaPrice: TextView = findViewById(R.id.pizzaPrice)
pizzaPrice.text = intent.getStringExtra("itemPrice").toString() /* Adding the price of the pizza to Screen. */
val smallPrice: String = pizzaPrice.text.toString().substring(startIndex = 0,endIndex = 4) //"10.8-12.8"
val largePrice: String = pizzaPrice.text.toString().substring(startIndex = 5,endIndex = 9)
val pizzaSize: TextView = findViewById(R.id.pizzaSize)
val topping1: EditText = findViewById(R.id.topping1Entry) /* Storing the View Topping 1 in the variable topping1. */
val topping2: EditText = findViewById(R.id.topping2Entry)
val topping3: EditText = findViewById(R.id.topping3Entry)
val topping4: EditText = findViewById(R.id.topping4Entry)
/* If the user clicks 10 inch then call the insertSize function and the setPrice function. */
tenInchButton.setOnClickListener {
tenInchButton.setTextColor(Color.rgb(136,27,50))
twelveInchButton.setTextColor(Color.WHITE)
Toast.makeText(this,"Ten inch selected",Toast.LENGTH_SHORT).show() /* Lets the user know. */
insertSize(size = SIZE_SMALL.toString(),pizzaSize = pizzaSize) /* Calling to insert the Size,passing the arguments SIZE_SMALL & the View pizzaSlice. */
setPrice(price = smallPrice,pizzaPrice = pizzaPrice) /* Calling to set** the price of the item,passing the price and the View pizzaPrice. */
}
/* If the user clicks 12 inch then call the insertSize function and the setPrice function. */
twelveInchButton.setOnClickListener {
tenInchButton.setTextColor(Color.WHITE)
twelveInchButton.setTextColor(Color.rgb(136,50))
Toast.makeText(this,"Twelve inch selected",Toast.LENGTH_SHORT).show() /* Lets the user know. */
insertSize(size = SIZE_LARGE.toString(),passing the arguments SIZE_LARGE & the View pizzaSlice. */
setPrice(price = largePrice,passing the price and the View pizzaPrice. */
}
/* If the user clicks additional button to charge for addition: check size and call function to changePrice. */
additionalButton.setOnClickListener {
when { /* Using when to see what Size the item is,if its a certain size the price is adjusted to suit this. */
pizzaSize.text == (SIZE_SMALL.toString()) -> {
changePrice(price = ADD_TOP_SMALL,textView = pizzaPrice)
Toast.makeText(this,"Charging for a Small Adding",Toast.LENGTH_SHORT).show() /* Lets the user whats being charged. */
} pizzaSize.text == (SIZE_LARGE.toString()) -> {
changePrice(price = ADD_TOP_LARGE,"Large Sized Additional Adding",Toast.LENGTH_SHORT).show() /* Lets the user whats being charged. */
} else -> { /* If the size hasn't been picked then change the colors of the inches and warn the user. */
tenInchButton.setTextColor(Color.rgb(202,180,156))
twelveInchButton.setTextColor(Color.rgb(202,156))
Toast.makeText(this,"Missing Size Choice!",Toast.LENGTH_SHORT).show() /* Lets the user know they need to pick a size before charging for toppings. */
}
}
}
/* If the user clicks premium button to charge for addition: check size and call function to changePrice. */
premiumButton.setOnClickListener {
when { /* Using when to see what Size the item is,if its a certain size the price is adjusted to suit this. */
pizzaSize.text == (SIZE_SMALL.toString()) -> {
changePrice(price = ADD_PREM_SMALL,"Small Sized Premium Topping",Toast.LENGTH_SHORT).show() /* Lets the user whats being charged. */
} pizzaSize.text == (SIZE_LARGE.toString()) -> {
changePrice(price = ADD_PREM_LARGE,textView = pizzaPrice)
Toast.makeText(this,"Large Sized Premium Adding",Toast.LENGTH_SHORT).show() /* Lets the user whats being charged. */
}
else -> { /* If the size hasn't been picked then change the colors of the inches and warn the user. */
tenInchButton.setTextColor(Color.rgb(202,Toast.LENGTH_SHORT).show() /* Lets the user know they need to pick a size before charging for toppings. */
}
}
}
/* completeBtn onClickListener,if pressed then load the entries and add them into an intent. Finish this Activity and return back to Order Activity. */
completeBtn.setOnClickListener {
val itemName: String = pizzaName.text.toString() /* Storing all the changes to the views that have been made. */
val itemPrice: String = pizzaPrice.text.toString()
val itemSize: String = pizzaSize.text.toString()
val top1: String = topping1.text.toString()
Toast.makeText(this,Toast.LENGTH_LONG).show() /* Lets the user whats being charged. */
if (itemSize == "10" || itemSize == "12") {
val intent = Intent() /* Creating an Intent. */
intent.putExtra("itemName",itemName)
intent.putExtra("itemPrice",itemPrice) /* Adding changes made to be sent back and read for the new entry in the RecyclerView. */
intent.putExtra("itemSize",itemSize)
intent.putExtra("topping1",top1)
setResult(Activity.RESULT_OK,intent) /* Setting the Result to pass the OK (-1) Result and including the intent with its data. */
startActivity(intent) /* Starting Activity. */
finish() /* Ending the Activity. */
} else { /* If the size hasn't been picked then change the colors of the inches and warn the user. */
tenInchButton.setTextColor(Color.rgb(202,156))
twelveInchButton.setTextColor(Color.rgb(202,156))
Toast.makeText(this,Toast.LENGTH_SHORT).show() /* Lets the user know they need to pick a size before finishing. */
}
}
}
/* Function to check if the User has entered information into toppings or not. */
private fun emptyAdditions(editText: EditText): Boolean {
val msg: String = editText.text.toString()
return msg.trim().isNotEmpty()
}
/* Changing the Size of the Item to the one selected. */
private fun insertSize(size: String,pizzaSize: TextView) {
pizzaSize.text = size
}
/* Setting the Price of the Item to the one selected. */
private fun setPrice(price: String,pizzaPrice: TextView) {
pizzaPrice.text = price
}
/* Changing the Price of the Item to the one selected. */
private fun changePrice(price: Double,textView: TextView) {
var cost = textView.text.toString()
var total = (cost.toDouble() + price)
total = String.format("%.2f",total).toDouble()
textView.text = total.toString()
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)