当我看到它不是时,为什么我的 resultLauncher.launch(intent) 返回 null?

问题描述

问题:

在调试期间,当“if”参数表示该值不等于(即!=)为零时,似乎 Trace.traceEnd(traceTag) 正在对“0”(零)值执行。请参阅下面的附加屏幕截图。因此,当 intentresultLauncher 都不为空时,它会返回一个“nullExceptionPointer”。

我在做什么:

我正在创建一个表,其中一行包含 TextView,当执行 TextView 的 OnClick 时,它启动 FileChooser —— 预期结果是捕获文件路径并在 TableRow 中填充 TextView。

当 FileChooser 类初始化并传递 Intent 时,我可以单步执行构造函数中的代码,看到 resultLauncher 已完全初始化并且传递的 Intent 不为 null。下面是点击操作和调用文件选择器类的代码 TableRow / TextView。

空异常指针出现在 resultLauncher.launch(intent) 上。单步执行时,它出现在 Looper.java 类中,if(traceTag !=0) 参数在为 0 时正在执行(参见图片附件)。

TableRow TextView 的代码 OnClick 设置:

            row.addView(addRowTextViewToTable(context,fileName,false));
            row.setClickable(true);
            row.setonClickListener(v ->{
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                FileChooser fileChooser = new FileChooser(intent);
                TextView child = (TextView) row.getChildAt(1);
                child.setText(fileChooser.getFilePath());
            });

FileChooser 类:

公共类 FileChooser 扩展 AppCompatActivity {

public FileChooser(Intent intent){
    resultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),result -> {
        if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null){
            Uri uri = result.getData().getData();
            filePath = uri.getPath();
        }
    });
    resultLauncher.launch(intent);
}

private String fileName;
private String filePath;
private final ActivityResultLauncher<Intent> resultLauncher;

public String getFileName() {
    return fileName;
}

public String getFilePath() {
    return filePath;
}

public ActivityResultLauncher<Intent> getResultLauncher() {
    return resultLauncher;
}

}

在我看来 Looper.java 类工作不正常:

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)