当前位置: 首页 > 知识库问答 >
问题:

找不到处理意图{(有附加)}错误的activity

齐健柏
2023-03-14

在activity A中,一旦点击了一个按钮,activity B就会为一个结果启动(startActivityForResult)。在activity B中,用户填写信息并点击一个按钮。单击此按钮后,会添加一些额外的信息,并调用setResult,传递RESULT_OK和意图。

我遇到的问题是将Edittext添加到意图中会导致一个错误消息,即没有处理文本的activity。有没有人知道为什么添加topping1会导致这个错误。

这是错误消息:

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 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, 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()
    }

}

共有1个答案

支淮晨
2023-03-14

完成结果的activity后,不需要添加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. */
 类似资料:
  • 我对这个android编程是新手。所以,现在当我想从一个活动转到另一个活动时,我遇到了一个问题。当我在模拟器中运行时,它显示MyDemo在按下按钮指向另一个页面后已经停止工作。我已经阅读和尝试了很少的解决方案张贴在类似的问题,但似乎不能解决问题。任何建议都会对我有很大帮助。谢谢你。 这是错误日志: 09-09 10:56:36.046 247 4-2489/com.example.dothis.d

  • 我正在编写一个程序,当一个特定的短信到达手机时,我的应用程序中的主要活动应该被调用。我已经注册了一个< code>BroadcastReceiver,调用该活动的意图在< code>onReceive()方法中。问题是,每次我发送这个特定的短信,我得到一个关闭的力量。在读取logcat时,我看到了下面的NullPoint异常: 但就我而言,一切都做对了。谁能告诉我问题在哪里?提前谢谢你。 以下是清

  • 我有一个活动,我们称之为,它有一个。在适配器的代码中 当实际单击editOptionButton时,我得到以下堆栈跟踪 因此,我不知道为什么会出现这个错误,也不知道我能做什么。你们中有谁可能知道为什么或者以前经历过吗?

  • 我有一个斜杠屏幕开始一个菜单,从我的菜单,我试图打开一个新的活动,尽管我得到了一个错误,当我这样做和应用程序崩溃。 我的清单 编辑:直接调用类工作:

  • 本附录提供了 Erlang 错误处理机制的细致总结。 匹配错误 当我们调用一个传入错误参数的内建函数时,参数不匹配的函数时,匹配错误就会产生。 当遇到匹配错误时,系统的行为可以描述成以下几种情形: if(called a BIF with bad args)then Error = badarg elseif(cannot and a matching function)then

  • 问题内容: 我一直在评估NOSTRA的Universal-Image-Loader库以异步下载图像并将其显示在ListView中。到目前为止,除了一个问题之外,它都可以正常工作。 有时,当列表滚动时,来自内存缓存的位图会附加错误。停止滚动后,将附加正确的图像。这种情况非常罕见,我找不到100%的方式来再现它。我上次拍摄视频时是这样。 这是代码,可以在此处找到UIL配置和方法。 我真的很感谢在此问题