没有找到Activity来处理Intent {(has extras)}错误



在活动A中,一旦按钮被单击,活动B将开始一个结果(startActivityForResult)。在活动B中,用户填写信息并单击按钮。一旦这个按钮被点击,一些额外的信息被添加和setResult被调用,传递RESULT_OK和意图。

我遇到的问题是Edittext被添加到意图导致一个错误消息,没有活动来处理文本。有谁知道为什么添加浇头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)

Activity 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 failedn") /* 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, price: String, size: String) {
Toast.makeText(this, "Item added!", Toast.LENGTH_SHORT).show() /* Toast Message to confirm insertion. */
val newItem = DataModel("$name", "$size", "$price", 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, price: String, size: String, top1: String, top2: String, top3: String, top4: String) {
Toast.makeText(this, "Item added!", Toast.LENGTH_SHORT).show() /* Toast Message to confirm insertion. */
val newItem = DataModel("$name", "$size", "$price", "$top1", "$top2", "$top3", "$top4", 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. */
}
private fun getItemsList(): ArrayList<DataModel> {
//list.add(DataModel("Romana","1","12.50", "Pepperoni", "Aubergine", "Ex Mozz.", "Salami", OrderAdapter.TOPPINGS_4))
//list.add(DataModel("American","1","12.50", 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. */
}
}

Activity 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,27,50))
Toast.makeText(this, "Twelve inch selected", Toast.LENGTH_SHORT).show() /* Lets the user know. */
insertSize(size = SIZE_LARGE.toString(), pizzaSize = pizzaSize) /* Calling to insert the Size, passing the arguments SIZE_LARGE & the View pizzaSlice. */
setPrice(price = largePrice, pizzaPrice = pizzaPrice) /* Calling to set** the price of the item, 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, textView = pizzaPrice)
Toast.makeText(this, "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,180,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, textView = pizzaPrice)
Toast.makeText(this, "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,180,156))
twelveInchButton.setTextColor(Color.rgb(202,180,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. */
}
}
}
/* 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, "$top1", 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,180,156))
twelveInchButton.setTextColor(Color.rgb(202,180,156))
Toast.makeText(this, "Missing Size Choice!", 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()
}
}

当你完成一个活动对于结果,您不需要添加startActivity(intent);,而是添加:

/*
rest of your code
*/
setResult(Activity.RESULT_OK, intent) /* Setting the Result to pass the OK (-1) 
Result and including the intent with its data. */
finish() /* Ending the  Activity. */

相关内容

最新更新