Search code examples
androidandroid-intentonactivityresultfilechooser

Why is my resultLauncher.launch(intent) returning null when I see that it is not?


Problem:

During debugging, it appears a Trace.traceEnd(traceTag) is executing on a '0' (zero) value when the 'if' argument says the value is not equal (i.e., !=) to zero. See attached screen shot below. As a result, it is returning a 'nullExceptionPointer' when neither the intent or resultLauncher is null.

What I'm doing:

I'm creating a table with a row containing a TextView in when the OnClick for the TextView is executed, it lauches a FileChooser -- and the intended result is to capture a file path and populate the TextView within the TableRow.

When the FileChooser class is initialized and the Intent is passed, I can step through the code within the constructor and see that the resultLauncher is fully initialized and the Intent passed is not null. Below is code TableRow / TextView on click action and call to the file chooser class.

The Null Exception Pointer occurs on the resultLauncher.launch(intent). While stepping through, it appears within the Looper.java class, the if(traceTag !=0) argument is executing when it is 0 (see pic attachement).

Code OnClick setup for TableRow TextView:

            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());
            });

The FileChooser Class:

public class FileChooser extends 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;
}

}

It appears to me the Looper.java class is not working correctly:

enter image description here


Solution

  • This ended up being an issue needing submitted to the Issue Tracker. It required a workaround. https://issuetracker.google.com/issues/19078322