Home>

Book: "Foster basic and applied skills firmly! Android application development textbook "
When I select it on the list screen displayed in "7.2: Screen transition of Android", the problem that the application is forcibly terminated cannot be solved and I am in trouble.

Error message
No error display.
The application is forcibly terminated by selecting the list.

Applicable source code

Language name: Kotlin
Source code
[MainActivity.kt]
package com.example.kenta.intentsample

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ListView
import android.widget.SimpleAdapter

class MainActivity: AppCompatActivity () {

override fun onCreate (savedInstanceState: Bundle?) {
super.onCreate (savedInstanceState)
setContentView (R.layout.activity_main)
// Get the screen component ListView
val lvMenu = findViewById(R.id.lvMenu)
// Prepare MutableList object to use with SimpleAdapter.
val menuList: MutableList>= mutableListOf ()
// Preparation of Map object that stores the data of "fried chicken set meal" and data registration to menuList.
var menu = mutableMapOf ("name" to "fried chicken set meal", "price" to "800 yen")
menuList.add (menu)
// Preparation of Map object for storing "hamburger set meal" data and registration of data to menuList.
menu = mutableMapOf ("name" to "Hamburg set meal", "price" to "850 yen")
menuList.add (menu)
// Preparation of Map object to store the data of "Ginger-grilled set meal" and data registration to menuList.
menu = mutableMapOf ("name" to "ginger-grilled set meal", "price" to "850 yen")
menuList.add (menu)
// Prepare a Map object to store the "steak set meal" data and register the data in the menuList.
menu = mutableMapOf ("name" to "Steak set meal", "price" to "1000 yen")
menuList.add (menu)
// Preparation of Map object to store data of "fried vegetables set meal" and data registration to menuList.
menu = mutableMapOf ("name" to "fried vegetables set meal", "price" to "750 yen")
menuList.add (menu)
// Preparation of Map object that stores "Tonkatsu set meal" data and data registration to menuList.
menu = mutableMapOf ("name" to "Tonkatsu set meal", "price" to "900 yen")
menuList.add (menu)
// Preparation of Map object to store data of "Minchi and set meal" and data registration to menuList.
menu = mutableMapOf ("name" to "Minchi and set meal", "price" to "850 yen")
menuList.add (menu)
// Preparation of Map object for storing "Chicken cutlet set meal" data and data registration to menuList.
menu = mutableMapOf ("name" to "chicken cutlet set meal", "price" to "900 yen")
menuList.add (menu)
// Preparation of Map object to store the data of "croquette set meal" and data registration to menuList.
menu = mutableMapOf ("name" to "croquette set meal", "price" to "850 yen")
menuList.add (menu)
// Preparation of Map object to store the data of "Grilled fish set meal" and data registration to menuList.
menu = mutableMapOf ("name" to "Grilled fish set meal", "price" to "750 yen")
menuList.add (menu)

// SimpleAdapter 4th argument From data is prepared.
val from = arrayOf ("name", "price")
// SimpleAdapter 5th argument to data preparation.
val to = intArrayOf (android.R.id.text1, android.R.id.text2)
// Create SimpleAdapter.
val adapter = SimpleAdapter (applicationContext, menuList, android.R.layout.simple_list_item_2, from, to)
// Adapter registration.
lvMenu.adapter = adapter

// List tap listener class registration
lvMenu.onItemClickListener = ListItemClickListener ()
}

// A member class that describes the processing when the list is tapped.
private inner class ListItemClickListener: AdapterView.OnItemClickListener {
override fun onItemClick (parent: AdapterView<*>, view: View, position: Int, id: Long) {
// Get the tapped row data. In SimpleAdapter, one row of data is MutableMap type!
val item = parent.getItemIdAtPosition (position) as MutableMap
// Get set meal name and amount.
val menuName = item ["name"]
val menuPrice = item ["price"]
// Create an intent object.
val intent = Intent (applicationContext, MenuThanksActivity :: class.java)
// Store the data to be sent to the 2nd screen.
intent.putExtra ("menuName", menuName)
intent.putExtra ("menuPrice", menuPrice)
// Start the second screen.
startActivity (intent)
}
}

}

[MenuThanksActivity.kt]
package com.example.kenta.intentsample

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView

class MenuThanksActivity: AppCompatActivity () {

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

// Get the data passed from the list screen.
val menuName = intent.getStringExtra ("menuName")
val menuPrice = intent.getStringExtra ("menuPrice")

// Get a TextView that displays the set meal name and amount.
val tvMenuName = findViewById(R.id.tvMenuName)
val tvMenuPrice = findViewById(R.id.tvMenuPrice)

// Display set meal name and amount in TextView.
tvMenuName.text = menuName
tvMenuPrice.text = menuPrice
}

// Process when tapping the back button.
fun onBackButtonClick (view: View) {
finish ()
}
}

[Activity_main.xml]

xmlns: android = "http://schemas.android.com/apk/res/android"
android: id = "@ + id/lvMenu"
android: layout_width = "match_parent"
android: layout_height = "match_parent" />

[Activity_menu_thanks.xml]

xmlns: android = "http://schemas.android.com/apk/res/android"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: orientation = "vertical">

android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: layout_marginBottom = "10dp"
android: layout_gravity = "center"
android: text = "@ string/tv_thx_title"
android: textSize = "25sp" />
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: layout_marginBottom = "10dp"
android: text = "@ string/tv_thx_desc"
android: textSize = "15sp" />
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: orientation = "horizontal">
android: id = "@ + id/tvMenuName"
android: layout_width = "0dp"
android: layout_height = "wrap_content"
android: layout_weight = "1" />
android: id = "@ + id/tvMenuPrice"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />

android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: onClick = "onBackButtonClick"
android: text = "@ string/bt_thx_back" />

What I tried

I intended to paste the source code, but I could not paste it.
I'm sorry.

Supplemental information (FW/tool version, etc.)

Mac version AndroidStudi 3.6.3

  • Answer # 1

      

    No error display.

    Although it is described, when browsing LogCat of Android Studio, the following error should be output.

    2020-05-05 17: 36: 21.830 15697-15697/com.example.kenta.intentsample E/InputEventReceiver: Exception dispatching input event.
    2020-05-05 17: 36: 21.830 15697-15697/com.example.kenta.intentsample E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
    2020-05-05 17: 36: 21.831 15697-15697/com.example.kenta.intentsample E/MessageQueue-JNI: java.lang.ClassCastException: java.lang.Long cannot be cast to java.util.Map
            at com.example.kenta.intentsample.MainActivity $ListItemClickListener.onItemClick (MainActivity.kt: 68)
            at android.widget.AdapterView.performItemClick (AdapterView.java:318)
            at android.widget.AbsListView.performItemClick (AbsListView.java:1159)
            at android.widget.AbsListView $PerformClick.run (AbsListView.java:3136)
            at android.widget.AbsListView.onTouchUp (AbsListView.java:4064)
            at android.widget.AbsListView.onTouchEvent (AbsListView.java:3822)
            at android.view.View.dispatchTouchEvent (View.java:12513)
            at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3024)
            at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2705)
            at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3030)
            at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2719)
            at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3030)
            at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2719)
            at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3030)
            at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2719)
            at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3030)
            at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2719)
            at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3030)
            at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2719)
            at com.android.internal.policy.DecorView.superDispatchTouchEvent (DecorView.java:440)
            at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent (PhoneWindow.java:1830)
            at android.app.Activity.dispatchTouchEvent (Activity.java:3400)
            at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent (WindowCallbackWrapper.java:69)
            at com.android.internal.policy.DecorView.dispatchTouchEvent (DecorView.java:398)翻译不全at android.widget.AdapterView.performItemClick (AdapterView.java:318)
            at android.widget.AbsListView.performItemClick (AbsListView.java:1159)
            at android.widget.AbsListView $PerformClick.run (AbsListView.java:3136)
            at android.widget.AbsListView.onTouchUp (AbsListView.java:4064)
            at android.widget.AbsListView.onTouchEvent (AbsListView.java:3822)
            at android.view.View.dispatchTouchEvent (View.java:12513)
            at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3024)
            at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2705)
            at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3030)
            at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2719)
            at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3030)
            at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2719)
            at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3030)
            at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2719)
            at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3030)
            at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2719)
            at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:3030)
            at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2719)
            at com.android.internal.policy.DecorView.superDispatchTouchEvent (DecorView.java:440)
            at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent (PhoneWindow.java:1830)
            at android.app.Activity.dispatchTouchEvent (Activity.java:3400)
            at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent (WindowCallbackWrapper.java:69)
            at com.android.internal.policy.DecorView.dispatchTouchEvent (DecorView.java:398)
            at android.view.View.dispatchPointerEvent (View.java:12752)
            at android.view.ViewRootImpl $ViewPostImeInputStage.processPointerEvent (ViewRootImpl.java:5106)
            at android.view.ViewRootImpl $ViewPostImeInputStage.onProcess (ViewRootImpl.java:4909)
            at android.view.ViewRootImpl $InputStage.deliver (ViewRootImpl.java:4426)
            at android.view.ViewRootImpl $InputStage.onDeliverToNext (ViewRootImpl.java:4479)
            at android.view.ViewRootImpl $InputStage.forward (ViewRootImpl.java:4445)
            at android.view.ViewRootImpl $AsyncInputStage.forward (ViewRootImpl.java:4585)
            at android.view.ViewRootImpl $InputStage.apply (ViewRootImpl.java:4453)翻译不全

Related articles