発生している問題・エラーメッセージ
連絡先を起動し、リストを選択するとシャットダウンしてしまう エラーメッセージ不明 ずっと正常に動いてましたが、急に動かなくなってしまいました。 修正箇所のご教授お願いします。
該当のソースコード
kotlin
1ContactPicker.kt 2 3import android.app.Activity 4import android.content.Intent 5import android.database.Cursor 6import android.net.Uri 7import android.os.Bundle 8import android.provider.ContactsContract 9import android.view.LayoutInflater 10import android.view.View 11import android.view.ViewGroup 12import androidx.appcompat.app.AppCompatActivity 13import androidx.fragment.app.Fragment 14 15data class PickedContact(val number: String, val name: String?) 16 17class ContactPicker constructor(private val requestCode: Int = 23) : Fragment() { 18 19 private lateinit var onContactPicked: (PickedContact) -> Unit 20 private lateinit var onFailure: (Throwable) -> Unit 21 22 companion object { 23 24 private const val TAG = "ContactPicker" 25 26 fun create( 27 activity: AppCompatActivity, 28 onContactPicked: (PickedContact) -> Unit, 29 onFailure: (Throwable) -> Unit 30 ): ContactPicker? { 31 32 return try { 33 val picker = ContactPicker() 34 picker.onContactPicked = onContactPicked 35 picker.onFailure = onFailure 36 activity.supportFragmentManager.beginTransaction() 37 .add(picker, TAG) 38 .commitNowAllowingStateLoss() 39 40 picker 41 } catch (e: Exception) { 42 onFailure(e) 43 null 44 } 45 } 46 } 47 48 override fun onCreate(savedInstanceState: Bundle?) { 49 super.onCreate(savedInstanceState) 50 retainInstance = true 51 } 52 53 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 54 return null 55 } 56 57 fun pick() { 58 try { 59 Intent().apply { 60 data = ContactsContract.CommonDataKinds.Phone.CONTENT_URI 61 action = Intent.ACTION_PICK 62 startActivityForResult(this, requestCode) 63 } 64 } catch (e: Exception) { 65 onFailure(e) 66 } 67 } 68 69 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 70 super.onActivityResult(requestCode, resultCode, data) 71 if (resultCode == Activity.RESULT_OK) { 72 when (requestCode) { 73 this.requestCode -> { 74 var cursor: Cursor? = null 75 try { 76 cursor = data?.data.let { uri -> 77 uri as Uri 78 activity?.contentResolver?.query(uri, null, null, null, null) 79 } 80 cursor?.let { 81 it.moveToFirst() 82 val phoneNumber = 83 it.getString(it.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace("-","") 84 val name = 85 it.getString(it.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)) 86 it.close() 87 onContactPicked(PickedContact(phoneNumber, name)) 88 } 89 90 } catch (e: Exception) { 91 onFailure(e) 92 cursor?.close() 93 } 94 } 95 } 96 } 97 } 98} 99MainActivity.kt 100 fun setContactPicker(NameText: TextView, NumberText: TextView){ 101 val contactPicker: ContactPicker? = ContactPicker.create( 102 activity = this, 103 onContactPicked = { 104 NameText.text = Editable.Factory.getInstance().newEditable("${it.name}") 105 NumberText.text = Editable.Factory.getInstance().newEditable("${it.number}") 106 }, 107 onFailure = { NameText.text = Editable.Factory.getInstance().newEditable(it.localizedMessage) } 108 ) 109 contactPicker?.pick() 110 } 111
あなたの回答
tips
プレビュー