如何解决“请确保先调用FirebaseApp.initializeApp(Context)”

时间:2019-07-01 08:38:52

标签: java android

本课程我使用了OCR firebase形式 https://www.youtube.com/watch?v=_rO7A2ZHS3o

但是当我单击“检测文本”时出现此错误

Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.atronteam.scandoctors. Make sure to call FirebaseApp.initializeApp(Context) first.

我在onCreate方法中使用了FirebaseApp.initializeApp(this),但存在错误

  

使用的版本:   firebase-ml-vision:16.0.0,   android Studio:3.4.1,   最低SDk版本:19

public class MainActivity extends AppCompatActivity {


    ImageView imageView;
    TextView textView;
    Bitmap bitmap;
    private static final int SELECT_PHOTO = 100;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FirebaseApp.initializeApp(this);

        imageView = (ImageView) findViewById(R.id.imageView);
        textView = (TextView) findViewById(R.id.textView);


    }

    public void detect(View view) {
        if (bitmap == null) {
            Toast.makeText(getApplicationContext(), "Bitmap is null", Toast.LENGTH_SHORT).show();

        } else {
            FirebaseVisionImage firebaseVisionImage = FirebaseVisionImage.fromBitmap(bitmap);

            FirebaseVisionTextDetector firebaseVisionTextDetector = FirebaseVision.getInstance().getVisionTextDetector();

            firebaseVisionTextDetector.detectInImage(firebaseVisionImage)
                    .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                        @Override
                        public void onSuccess(FirebaseVisionText firebaseVisionText) {
                             process_text(firebaseVisionText);
                        }
                    });
        }
    }

    private void process_text(FirebaseVisionText firebaseVisionText) {
        List<FirebaseVisionText.Block> blocks = firebaseVisionText.getBlocks();
        if (blocks.size() == 0){
            Toast.makeText(getApplicationContext(),"No Text detected",Toast.LENGTH_SHORT).show();
        }
        else {
            for (FirebaseVisionText.Block block:firebaseVisionText.getBlocks()){
                String text = block.getText();
                textView.setText(text);
            }
        }
    }

    public void pick_image(View view) {
        Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(i, SELECT_PHOTO);
    }


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

        switch(requestCode) {
            case SELECT_PHOTO:
                if(resultCode == RESULT_OK){
                    Uri selectedImage = data.getData();
                    try {
                        bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),selectedImage);
                        imageView.setImageBitmap(bitmap);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                }
        }
    }

}
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:5205) 
    at android.view.View$PerformClick.run(View.java:21181) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5487) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:765) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:655) 
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.atronteam.scandoctors. Make sure to call FirebaseApp.initializeApp(Context) first.
    at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@17.1.0:186)
    at com.google.firebase.ml.vision.FirebaseVision.getInstance(Unknown Source)
    at com.atronteam.scandoctors.MainActivity.detect(MainActivity.java:57)
    at java.lang.reflect.Method.invoke(Native Method) 
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
    at android.view.View.performClick(View.java:5205) 
    at android.view.View$PerformClick.run(View.java:21181) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5487) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:765) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:655)

0 个答案:

没有答案