0

i've made a recipe app recently but i'm having problems when uploading the recipe, as i soon as i tap the upload button the emulator goes back to home and shows me a bunch of error messages, i hope someone can help me with this problem.

I'm getting this error:

E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
    SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
    SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.
W/NetworkRequest: no auth token for request
E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.
W/NetworkRequest: no auth token for request
E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.
W/NetworkRequest: no auth token for request
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.recipeapp, PID: 8225
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.DialogFragment.dismiss()' on a null object reference
        at com.example.recipeapp.Upload_Recipe$2.onSuccess(Upload_Recipe.java:86)
        at com.example.recipeapp.Upload_Recipe$2.onSuccess(Upload_Recipe.java:77)
        at com.google.firebase.storage.StorageTask.lambda$new$0(com.google.firebase:firebase-storage@@16.0.4:125)
        at com.google.firebase.storage.StorageTask$$Lambda$1.raise(Unknown Source:6)
        at com.google.firebase.storage.TaskListenerImpl.lambda$onInternalStateChanged$2(com.google.firebase:firebase-storage@@16.0.4:94)
        at com.google.firebase.storage.TaskListenerImpl$$Lambda$3.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/Process: Sending signal. PID: 8225 SIG: 9

And this is my Upload_Recipe Activity code:

package com.example.recipeapp;

import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import java.text.DateFormat;
import java.util.Calendar;

public class Upload_Recipe extends AppCompatActivity {

    ImageView recipeImage;
    Uri uri;
    EditText txt_name, txt_description, txt_price;
    String imageUrl;
    private DialogFragment progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload__recipe);

        recipeImage = (ImageView) findViewById(R.id.iv_foodImage);
        txt_name = (EditText) findViewById(R.id.txt_recipe_name);
        txt_description = (EditText) findViewById(R.id.text_description);
        txt_price = (EditText) findViewById(R.id.text_price);


    }

    public void btnSelectImage(View view) {

        Intent photoPicker = new Intent(Intent.ACTION_PICK);
        photoPicker.setType("image/*");
        startActivityForResult(photoPicker, 1);

    }

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

        if (resultCode == RESULT_OK) {

            uri = data.getData();
            recipeImage.setImageURI(uri);

        } else Toast.makeText(this, "Você não selecionou uma imagem", Toast.LENGTH_SHORT).show();
    }

    public void uploadImage() {

        StorageReference storageReference = FirebaseStorage.getInstance()
                .getReference()
                .child("RecipeImage")
                .child(uri.getLastPathSegment());

        storageReference.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                Task<Uri> uriTask = taskSnapshot.getStorage().getDownloadUrl();
                while (!uriTask.isComplete()) ;
                Uri urlImage = uriTask.getResult();
                imageUrl = urlImage.toString();
                uploadRecipe();
                progressDialog.dismiss();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                progressDialog.dismiss();
            }
        });
    }

    public void btnUploadRecipe(View view) {

        uploadImage();

    }

    public void uploadRecipe() {

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Enviando Receita...");
        progressDialog.show();
        progressDialog.dismiss();
        finish();

        FoodData foodData = new FoodData(txt_name.getText().toString()
                , txt_description.getText().toString(), txt_price.getText().toString()
                , imageUrl);

        String myCurrentDateTime = DateFormat.getDateTimeInstance()
                .format(Calendar.getInstance().getTime());

        FirebaseDatabase.getInstance()
                .getReference("Recipe")
                .child(myCurrentDateTime).setValue(foodData).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {

                if (task.isSuccessful()) {

                    Toast.makeText(Upload_Recipe.this, "Receita Enviada", Toast.LENGTH_SHORT).show();
                    finish();

                }

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(Upload_Recipe.this, "Falha ao Enviar", Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }
        });
    }
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Júlio Sordi
  • 31
  • 1
  • 5
  • It looks like `progressDialog` is not always initialized when you call methods on it. To learn how to fix this, see https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Feb 29 '20 at 20:54
  • Yes this helped me a lot though Dialogfragment is incompatible with object, i had set it as private fragment because i were getting errors at progressDialog.dismiss(); if i set it as string object or anything it can't resolve method dismiss – Júlio Sordi Mar 01 '20 at 13:51

0 Answers0