尝试从手机读取联系人时 Java 应用程序崩溃

问题描述 投票:0回答:1

我的应用程序在尝试读取联系人时崩溃。错误信息是

java.lang.RuntimeException:无法启动活动 ComponentInfo{com.ccoders.contactlistjava/com.ccoders.contactlistjava.MainActivity}:android.database.StaleDataException:尝试在关闭后访问光标。

我首先认为这是读取联系人权限的问题。但读取通讯录好像有问题。

这是代码:

public class MainActivity extends AppCompatActivity {

    // Initialize variable
    RecyclerView recyclerView;
    ArrayList<ContactModel> arrayList = new ArrayList<ContactModel>();
    MainAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        // requesting to the user for permission.

        //checking whether the read contact permission is granted.
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {

            System.out.println("Permission is not granted");
            // requesting to the user for permission.
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 100);

        } else {
            System.out.println("Permission is granted");
            //if app already has permission this block will execute.
            getContactList();
        }
    }
    // if the user clicks ALLOW in dialog this method gets called.
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        getContactList();
    }


    private void getContactList() {

        // Initialize uri
        Uri uri = Contacts.CONTENT_URI;

        // Sort by ascending
        String sort = CommonDataKinds.Phone.DISPLAY_NAME+" ASC";

        // Initialize cursor
        Cursor cursor = getContentResolver().query(
                uri, null, null, null, sort
        );

        // Check condition

        if (cursor.getCount() > 0) {
            // When cursor is greater than 0
            // Use while loop

            while(cursor.moveToNext()){
                // Cursor move to next
                // Get contact id

                String id = cursor.getString(cursor.getColumnIndexOrThrow(Contacts._ID));

                // Get contact name

                String name = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.DISPLAY_NAME
                ));
                // Initialze phone uri
                Uri uriPhone = CommonDataKinds.Phone.CONTENT_URI;
                // Initialize selection
                String selection = CommonDataKinds.Phone.CONTACT_ID + " =?";

                // Initialize phone cursor
                Cursor phoneCursor = getContentResolver().query(
                        uriPhone, null, selection, new String[]{id},null
                );
                // Check condition
                if (phoneCursor.moveToNext()){
                    // When phone cursor move to next
                    String number = phoneCursor.getString(phoneCursor.getColumnIndexOrThrow(
                            CommonDataKinds.Phone.NUMBER
                    ));
                    // Initialze contact model
                    ContactModel model = new ContactModel();
                    // Set name
                    model.setName(name);
                    // Set number
                    model.setNumber(number);
                    // Add model in array list
                    arrayList.add(model);
                    // Close phone cursor
                    phoneCursor.close();
                }

                // Close cursor
                cursor.close();
            }

            // Set layout manager
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            // Initialize adapter
            adapter = new MainAdapter(this,arrayList);
            // Set adapter
            recyclerView.setAdapter(adapter);
        }
    }
}

我该如何解决这个问题?

java android android-contacts
1个回答
0
投票
  1. 您正在使用 Android 中不支持的 System.out.println(),因为它没有控制台,因此请使用 Logger 来获取日志

  2. 在此之后,请提供android studio的日志输出,以便其他人更好地澄清以识别确切的错误。

© www.soinside.com 2019 - 2024. All rights reserved.