0

I want to add String to SQL database java Android I have while loop to get contacts from the phone

and when I insert the String the app crashes

my code is

public void getContactList() {
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);

if ((cur != null ? cur.getCount() : 0) > 0) {
    while (cur != null && cur.moveToNext()) {
        String id = cur.getString(
                cur.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur.getString(cur.getColumnIndex(
                ContactsContract.Contacts.DISPLAY_NAME));

        if (cur.getInt(cur.getColumnIndex(
                ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
            Cursor pCur = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                    new String[]{id}, null);
            while (pCur.moveToNext()) {
                String phoneNo = pCur.getString(pCur.getColumnIndex(
                        ContactsContract.CommonDataKinds.Phone.NUMBER));
                Log.i("jslkjclk",name+"\n"+phoneNo);
//the app crashes here
                sqLiteDatabase.execSQL("INSERT INTO Contacts (name,number) VALUES ('"+name+"','"+phoneNo+"')");
                sharedPreferences.edit().putString("IfContact","2").apply();
                contactIf=sharedPreferences.getString("IfContact","");
            }
            pCur.close();
        }
    }
}
if(cur!=null){
    cur.close();
}
}

please anyone help me

my logcat

11-23 22:57:25.812 12599-12599/? I/art: Late-enabling -Xcheck:jni
11-23 22:57:25.930 12599-12599/com.example.importantmessageonly W/System: ClassLoader referenced unknown path: /data/app/com.example.importantmessageonly-2/lib/arm64
11-23 22:57:26.090 12599-12599/com.example.importantmessageonly W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-23 22:57:26.207 12599-12599/com.example.importantmessageonly I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
11-23 22:57:26.207 12599-12599/com.example.importantmessageonly I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
11-23 22:57:26.406 12599-12599/com.example.importantmessageonly I/jslkjclk: no
11-23 22:57:26.444 12599-12599/com.example.importantmessageonly I/jslkjclk: Hassan
    01020499918
11-23 22:57:26.782 12599-12599/com.example.importantmessageonly I/Process: Sending signal. PID: 12599 SIG: 9

that's all my logcat I think because I working on physical device

  • can you provide the crash report? – Zain Nov 23 '20 at 19:58
  • 1
    Crashes are always accompanied by stacktraces that tell you what exception was raised and where. – luk2302 Nov 23 '20 at 19:58
  • To be honest I don't know how to get the crash report But I think it's because of my way of writing the code – Abdelftah Ahmad Zowail Nov 23 '20 at 20:26
  • See: [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/q/23353173/295004) – Morrison Chang Nov 23 '20 at 20:47
  • @AbdelftahAhmadZowail On the bottom row in Android Studio you should see an option called "Logcat". If you don't, go to the top bar and click View > Tool Windows > Logcat. When your app crashes Logcat will give you a detailed error message of what went wrong – TechRando Nov 23 '20 at 20:55
  • @TechRando look at the edit – Abdelftah Ahmad Zowail Nov 23 '20 at 21:02
  • Not really read in detail, but in while-cur loop collect what you need to insert and then loop inserting. – Joop Eggen Nov 23 '20 at 21:12
  • It's very difficult to debug a crash without a stack trace. See [Unfortunately MyApp has stopped. How can I solve this?](/q/23353173) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](/q/3988788) for advice on what to do once you have the stack trace. If you still need help, edit your question to include the **complete stack trace**, as well as **which line of your code** the stack trace points to. – Ryan M Nov 24 '20 at 10:53

0 Answers0