Я не знаю, как описать эту проблему, поэтому перечислю задачи, которые мне нужно выполнить для приложения:
- Загрузите файл .txt из Firebase Cloud Storage
- Сохраните его в моем хранилище, указанном в приложении c,
- Получите файл из хранилища, указанном в приложении c, и прочтите его построчно.
Logcat:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/0/1/content://media/external/images/media/2328/ORIGINAL/NONE/image/png/1631689393 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F2328/ORIGINAL/NONE/image%2Fpng/1631689393} }} to activity {com.example.android.getroasted/com.example.android.getroasted.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.lifecycle.MutableLiveData.observe(androidx.lifecycle.LifecycleOwner, androidx.lifecycle.Observer)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.lifecycle.MutableLiveData.observe(androidx.lifecycle.LifecycleOwner, androidx.lifecycle.Observer)' on a null object reference
Фрагмент:
private void setRoast() throws FileNotFoundException {
homeViewModel.getRandomRoast().observe(getViewLifecycleOwner(), new Observer<byte[]>() {
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
public void onChanged(byte[] bytes) {
try (FileOutputStream fos = requireContext().openFileOutput("roasts", Context.MODE_PRIVATE)) {
fos.write(bytes);
} catch (IOException e) {
Log.d(TAG, "IOException: " + e);
}
}
});
roastContainer.setVisibility(View.VISIBLE);
FileInputStream fis = getContext().openFileInput("roasts");
InputStreamReader inputStreamReader =
new InputStreamReader(fis, StandardCharsets.UTF_8);
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(inputStreamReader)) {
String line = reader.readLine();
while (line != null) {
stringBuilder.append(line).append('\n');
line = reader.readLine();
Log.d(TAG, "roast: " + line);
}
} catch (IOException e) {
// Error occurred when opening raw file for reading.
} finally {
String contents = stringBuilder.toString();
}
}
ViewModel:
public class HomeViewModel extends ViewModel {
private static final String TAG = "HomeViewModel";
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
StorageReference storageReference = firebaseStorage.getReference();
private StorageReference roastDocRef = storageReference.child("roasts.txt");
private MutableLiveData<byte[]> roastLiveData;
public MutableLiveData<byte[]> getRandomRoast() {
roastDocRef.getBytes(1024*1024)
.addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Log.d(TAG, "bytes: " + bytes);
roastLiveData.postValue(bytes);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "could not download the roast doc " + e);
}
});
return roastLiveData;
}
}
Я не знаю, почему и где возникает проблема, но я уже пробовал эти вещи:
- Проверил свои правила безопасности, они позволяют читать, писать
- обновлен viewmodel.setValue () TO viewmodel.postValue ()