Whenever I try to implement the ArrayAdapter on my ListView in my MainActivity.kt file, my app crashes. Once I comment it out, my app is able to run but, of course, it doesn't display the list of names that I want it.

This hasn't given me an issue before and even Googling it hasn't given me much insight into the problem I'm facing. I'm not sure if it may be caused due to me using Fragments as well as Activities. If anyone has any insights, it'd be greatly appreciated

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>

I expected the ListView of my contact names to be displayed as a List but, instead, my app just crashes instead

Related posts

Recent Viewed