i created my first extension with the help of chat gpt for personal use and build extension from Niotron IDE but when exporting app, the app crashes on start
the extension code is
package com.contact;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@DesignerComponent(
version = 1,
description = "Contact List",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "")
@SimpleObject(external = true)
public class Contact extends AndroidNonvisibleComponent {
public Contact(ComponentContainer container) {
super(container.$form());
}
@SimpleEvent(description = "Got Contact List")
public void GotContact(List<String> contactList) {
Log.d("ContactExtension", "GotContact event triggered with list size: " + contactList.size());
EventDispatcher.dispatchEvent(this, "GotContact", contactList);
}
@SimpleFunction(description = "Sort A to Z")
public void GetContact() {
final Context context = this.form.$context();
Log.d("ContactExtension", "Starting to retrieve contacts");
new Thread(new Runnable() {
@Override
public void run() {
final List<String> contactList = new ArrayList<>();
Cursor cursor = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER},
null, null, null);
if (cursor != null && cursor.getCount() > 0) {
Log.d("ContactExtension", "Found contacts: " + cursor.getCount());
HashMap<String, Set<String>> contactsMap = new HashMap<>();
while (cursor.moveToNext()) {
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumber = phoneNumber.replaceAll("\\s+", "");
if (!contactsMap.containsKey(contactName)) {
contactsMap.put(contactName, new HashSet<String>());
}
contactsMap.get(contactName).add(phoneNumber);
}
cursor.close();
for (String name : contactsMap.keySet()) {
String numbers = String.join(" ", contactsMap.get(name));
contactList.add(name + " - " + numbers);
}
Collections.sort(contactList);
form.runOnUiThread(new Runnable() {
@Override
public void run() {
GotContact(contactList);
}
});
} else {
Log.d("ContactExtension", "No contact found");
}
}
}).start();
}
}
can anyone fix it....