每当我尝试在MainActivity.kt文件中的ListView上实现ArrayAdapter时,我的应用程序就会崩溃。一旦我将其注释掉,我的应用程序就可以运行,但是,当然,它不会显示我想要的名称列表。
这以前没有给我一个问题,甚至谷歌搜索也没有给我很多我所面临的问题的见识。我不确定是否可能是由于我使用“片段”以及“活动”引起的。如果有人有任何见解,将不胜感激
main.kt:
package com.example.contactslist
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
/**
* A simple [Fragment] subclass.
*/
class main : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false)
}
}
MainActivity.kt:
package com.example.contactslist
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ListView
class MainActivity : AppCompatActivity() {
var CONTACT_DETAIL_ACTIVITY_REQUEST = 0
val names = ArrayList<String>()
val numbers = ArrayList<Int>()
// Variable to hold relationships
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
names.add("Baraka")
names.add("Blandy")
numbers.add(732)
numbers.add(666)
val contactsAdapter = ArrayAdapter(this, R.layout.activity_listview, names)
val listView1 = findViewById<ListView>(R.id.contact_list)
listView1.setAdapter(contactsAdapter)
val add_new_contact = findViewById<Button>(R.id.add_contacts)
add_new_contact.setOnClickListener {
val name = ""
val phone_number = 1
val relationship = ArrayList<String>()
val intent1 = Intent(this,ContactDetails::class.java)
startActivityForResult(intent1, CONTACT_DETAIL_ACTIVITY_REQUEST)
}
}
}
fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".main">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contacts"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
>
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/contact_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
<Button
android:id="@+id/delete_contacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Delete"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
>
</Button>
<Button
android:id="@+id/add_contacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
>
</Button>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment"
android:name="com.example.contactslist.main"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
activity_listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contact_list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<!-- <CheckBox-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- android:id="@+id/checked"-->
<!-- android:checked="false"/>-->
<TextView
android:id="@+id/contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"/>
</LinearLayout>
编辑:这是LogCat错误日志-
10-09 21:19:25.953 21333-21333 /? I / art:不是延迟启用-Xcheck:jni(已启用) 10-09 21:19:26.167 21333-21339 /?电子/艺术:无法向调试器发送回复:管道损坏 10-09 21:19:26.168 21333-21339 /? I / art:调试器不再处于活动状态 10-09 21:19:26.736 21333-21333 /? W /系统:ClassLoader引用的未知路径:/data/app/com.example.contactslist-1/lib/x86 10-09 21:19:27.245 21333-21333 / com.example.contactslist W / art:在Android 4.1之前,方法android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter,android .content.res.ColorStateList,android.graphics.PorterDuff $ Mode)会错误地覆盖android.graphics.drawable.Drawable中的package-private方法 10-09 21:19:27.618 21333-21333 / com.example.contactslist I / art:拒绝对先前失败的类java.lang.Class进行重新初始化 10-09 21:19:27.618 21333-21333 / com.example.contactslist I / art:拒绝对先前失败的类java.lang.Class进行重新初始化 10-09 21:19:27.725 21333-21333 / com.example.contactslist W / art:无效androidx.core.view.ViewCompat.dispatchFinishTemporaryDetach(android.view.View)的验证耗时103.153ms 10-09 21:19:27.972 21333-21343 / com.example.contactslist I / art:释放了背景部分并发标记清除GC的2073(241KB)AllocSpace对象,0(0B)LOS对象,52%的空闲对象,925KB / 1949KB,暂停1.940ms共150.819ms 10-09 21:19:28.074 21333-21370 / com.example.contactslist D / OpenGLRenderer:使用EGL_SWAP_BEHAVIOR_PRESERVED:true 10-09 21:19:28.268 21333-21333 / com.example.contactslist E / ArrayAdapter:您必须为TextView提供资源ID 10-09 21:19:28.269 21333-21333 / com.example.contactslist D / Android运行时:关闭VM 10-09 21:19:28.271 21333-21333 / com.example.contactslist E / Android运行时:致命异常:主要 流程:com.example.contactslist,PID:21333 java.lang.IllegalStateException:ArrayAdapter要求资源ID为TextView 在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:393) 在android.widget.ArrayAdapter.getView(ArrayAdapter.java:369) 在android.widget.AbsListView.obtainView(AbsListView.java:2346) 在android.widget.ListView.measureHeightOfChildren(ListView.java:1280) 在android.widget.ListView.onMeasure(ListView.java:1188) 在android.view.View.measure(View.java:18788) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 在android.view.View.measure(View.java:18788) 在androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227) 在androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572) 在android.view.View.measure(View.java:18788) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 在android.view.View.measure(View.java:18788) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143) 在android.view.View.measure(View.java:18788) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 在android.view.View.measure(View.java:18788) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在android.view.View.measure(View.java:18788) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 在android.view.View.measure(View.java:18788) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在com.android.internal.policy.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2643) 在android.view.View.measure(View.java:18788) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6013) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858) 在android.view.Choreographer.doCallbacks(Choreographer.java:670) 在android.view.Choreographer.doFrame(Choreographer.java:606) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 原因:java.lang.ClassCastException:android.widget.LinearLayout无法转换为android.widget.TextView 在android.widget.ArrayAdapter.create
答案 0 :(得分:3)
要修复您在activity_listview.xml中的崩溃 替换
android:id="@+id/contact_name"
使用
android:id="@android:id/text1"
答案 1 :(得分:0)
要解决此问题,请替换代码中的以下行。
val contactsAdapter = ArrayAdapter(this, R.layout.activity_listview, names)
使用
val contactsAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, names)
答案 2 :(得分:0)
崩溃可能是因为ArrayAdpater
期望TextView
,但是您通过了LinearLayout
:
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView at android.widget.ArrayAdapter.create
在您的activity_listview.xml
中,您需要删除包裹LinearLayout
的{{1}}:
activity_listview.xml:
TextView