【问题标题】:cursorLoader in androidstudioandroid studio中的cursorLoader
【发布时间】:2021-12-06 23:31:12
【问题描述】:

我是一个超级初学者,所以我只是在关注一些项目,我知道 startActivityforResult 已被弃用,所以我使用 ActivityResultLauncher 更改了代码。但我不知道如何修复 CursorLoader 错误。能告诉我怎么解决吗?

原来的代码是这样的。

 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        
        if(requestCode == PICK_PROFILE_FROM_ALBUM && resultCode == RESULT_OK) {

            String[] proj = {MediaStore.Images.Media.DATA};

            CursorLoader cursorLoader = new CursorLoader(this, data.getData(), proj, null, null, null);
            


            Cursor cursor = cursorLoader.loadInBackground();
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();

            //image path
            String photoPath = cursor.getString(column_index);

           
    }

这是当前代码

ActivityResultLauncher<Intent> startActivityResult = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    
                    if(result.getResultCode() == Activity.RESULT_OK) {
                        Intent data = result.getData();
                        String[] proj = {MediaStore.Images.Media.DATA};

                        CursorLoader cursorLoader = new CursorLoader(this, data.getData(), proj, null, null, null);
                       

                        Cursor cursor = cursorLoader.loadInBackground();
                        int column_index =cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                        cursor.moveToFirst();

                        
                        String photoPath = cursor.getString(column_index);
 }

所以 CursorLoader 中的上下文应该是固定的。它需要 'Context' 但现在是 ActivityResultCallback。

【问题讨论】:

    标签: java android android-cursorloader


    【解决方案1】:

    发生这种情况是因为在这种情况下this 关键字应该是指当前上下文,而当前上下文是匿名类,即ActivityResultCallback 的实例,但 CursorLoader 需要引用 ContextActivity 的实例。在这种情况下,简单的解决方法是将.this 附加到您的类或活动的名称中

    假设你的ActivityMainActivity,所以这个

    CursorLoader cursorLoader = new CursorLoader(this, data.getData(), proj, null, null, null);
    

    会转向这个

    CursorLoader cursorLoader = new CursorLoader(MainActivity.this, data.getData(), proj, null, null, null);
    

    【讨论】:

    • 很好解释@gtxtreme
    • 我同意,无论如何,这应该是公认的答案,很高兴我能帮助你
    【解决方案2】:

    不要写this,而是直接写youractivityname.this

    假设您的活动名称为 MainActivity,您应该这样传递上下文:

    CursorLoader cursorLoader = new CursorLoader(MainActivity.this, data.getData(), proj, null, null, null);
            
    

    【讨论】:

      【解决方案3】:

      这是长期运行项目的典型情况,有时您需要将现有代码库迁移到更新的 API。

      关于 CursorLoader。如果startActivityResult Launcher 是您活动中的字段 这一行

      CursorLoader cursorLoader = new CursorLoader(this, data.getData(), proj, null, null, null);
      

      可以替换为

      CursorLoader cursorLoader = new CursorLoader(YourActivityClass.this, data.getData(), proj, null, null, null);
      

      我还看到光标已加载到 UI 线程上,它可能会影响性能

      【讨论】:

      • 哦,好的,谢谢!
      猜你喜欢
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多