“不幸的是,应用程序已停止” java.lang.RuntimeException:无法启动活动ComponentInfo CAMERA APP

时间:2018-08-27 09:13:01

标签: java android xml android-layout android-studio

因此,我一直在YouTube上为我们的应用程序关注本教程,由于我几乎不了解任何应用程序开发(自学)知识,因此我手动键入了该代码,以便知道哪些行包含这些代码,并且...当我跟踪视频中的所有内容时(或者我可能错过了一些内容),我不知道它有什么问题。我的应用无法运行。

npm install @types/react-navigation

// OpenCamera.java     软件包e.alasse.opencv;

`08-27 16:50:41.336 4192-4192/e.alasse.opencv E/MultiWindowProxy: getServiceInstance failed!
08-27 16:50:41.818 4192-4192/e.alasse.opencv E/AndroidRuntime: FATAL EXCEPTION: main
Process: e.alasse.opencv, PID: 4192
java.lang.RuntimeException: Unable to start activity ComponentInfo{e.alasse.opencv/e.alasse.opencv.OpenCamera}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class ImageView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5728)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
 Caused by: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class ImageView
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
    at e.alasse.opencv.OpenCamera.onCreate(OpenCamera.java:33)
    at android.app.Activity.performCreate(Activity.java:6301)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:207) 
    at android.app.ActivityThread.main(ActivityThread.java:5728) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
 Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class ImageView
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
    at e.alasse.opencv.OpenCamera.onCreate(OpenCamera.java:33) 
    at android.app.Activity.performCreate(Activity.java:6301) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:207) 
    at android.app.ActivityThread.main(ActivityThread.java:5728) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
 Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f070066
    at android.content.res.Resources.getValue(Resources.java:1544)
    at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:331)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
    at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
    at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:59)
    at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:78)
    at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
    at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
    at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
    at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
    at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
    at e.alasse.opencv.OpenCamera.onCreate(OpenCamera.java:33) 
    at android.app.Activity.performCreate(Activity.java:6301) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:207) 
    at android.app.ActivityThread.main(ActivityThread.java:5728) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) `

// activity_open_camera.xml

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.ImageView;

import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.io.IOException;

public class OpenCamera extends AppCompatActivity {


ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_open_camera);
    iv = findViewById(R.id.imageView);
}

public void openGallery(View v) {
    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 101);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 101 && resultCode == RESULT_OK && data != null) {
        Uri imageUri = data.getData();
        String path = getPath(imageUri);
        loadImage(path);
        displayImage(sampledImg);


    }
}

private void displayImage(Mat mat)
{
    Bitmap bitmap = Bitmap.createBitmap(mat.cols(),mat.rows(),Bitmap.Config.RGB_565);

    //convert mat to bitmap

    Utils.matToBitmap(mat, bitmap);
    iv.setImageBitmap(bitmap);
}
    Mat sampledImg;
    private void loadImage(String path) {


    Mat originalImage = Imgcodecs.imread(path);
    Mat rgbImg = new Mat();

    Imgproc.cvtColor(originalImage, rgbImg, Imgproc.COLOR_BGR2RGB);
    Display display = getWindowManager().getDefaultDisplay();

    Point size = new Point();
    display.getSize(size);

    int mobile_width = (int) size.x;
    int mobile_height = (int) size.y;

    Mat sampledImg = new Mat();

    double downSampleRatio = calculateSubSimpleSize(rgbImg, mobile_width, mobile_height);
    Imgproc.resize(rgbImg, sampledImg, new Size(), downSampleRatio, downSampleRatio, Imgproc.INTER_AREA);

    try
    {
        ExifInterface exif = new ExifInterface(path);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

        switch(orientation)
        {
            case ExifInterface.ORIENTATION_ROTATE_90:
                sampledImg = sampledImg.t();
                Core.flip(sampledImg, sampledImg, 1);
                break;

            case ExifInterface.ORIENTATION_ROTATE_270:
                sampledImg = sampledImg.t();
                Core.flip(sampledImg, sampledImg, 0);
                break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}



private double calculateSubSimpleSize(Mat src, int mobile_width, int mobile_height) {

    final int width = src.width();
    final int height = src.height();
    double inSampleSize = 1;

    if (height > mobile_height || width > mobile_width) {
//calculate ratio

        final double heightRatio = (double) mobile_height / (double) height;
        final double widthRatio = (double) mobile_width / (double) width;

        inSampleSize = heightRatio < widthRatio ? height : width;
    }

    return inSampleSize;
}

private String getPath(Uri uri) {
    if (uri == null) {
        return null;

    } else {
        String projection = (MediaStore.Images.Media.DATA);
        Cursor cursor = getContentResolver().query(uri, new String[]{projection}, null, null, null);
        if (cursor != null) {
            int col_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();

            return cursor.getString(col_index);
        }

    }
    return uri.getPath();
}
}

//清单

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OpenCamera">

<Button
    android:id="@+id/button"
    android:layout_width="351dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@color/bgcolor"
    android:onClick="openGallery"
    android:text="Pick Image"
    android:textColor="@color/txtcolor" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="351dp"
    android:layout_height="562dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="0dp"
    android:background="@color/bgcolor"
    app:srcCompat="@drawable/ic_launcher_foreground" />

1 个答案:

答案 0 :(得分:1)

尝试替换以下属性

app:srcCompat="@drawable/ic_launcher_foreground" />

在您的

  

activity_open_camera.xml

位于“ ImageView”元素底部的文件,

android:src="@drawable/ic_launcher_foreground"/>

让我知道它是否有助于避免应用程序的ANR状态。