0

Hello Everyone Thanks for reading my question

I tried to make an application which if the button in a main_activity[main view] get a click signal, Call another Activity View with Camera SurfaceView on it.

But, I've seen an 'Unable to start activity component' error in the part where I keep calling up the activity_camera.xml document.

Even though I looked up and applied various postings related to the error, I still didn't know where to go wrong and how to fix it, so I take the liberty of asking you a question.

Here are my errors and codes

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.colorcomp2, PID: 27039
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.colorcomp2/com.example.colorcomp2.CameraActivity}: android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class com.example.colorcomp2.CameraSurface
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class com.example.colorcomp2.CameraSurface
     Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.colorcomp2.CameraSurface
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        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 androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.example.colorcomp2.CameraActivity.onCreate(CameraActivity.java:24)
        at android.app.Activity.performCreate(Activity.java:7183)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: java.lang.RuntimeException: Fail to connect to camera service
        at android.hardware.Camera.<init>(Camera.java:510)
        at android.hardware.Camera.open(Camera.java:374)
        at com.example.colorcomp2.CameraSurface.<init>(CameraSurface.java:29)
        at java.lang.reflect.Constructor.newInstance0(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:647) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 
        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 androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555) 
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161) 
        at com.example.colorcomp2.CameraActivity.onCreate(CameraActivity.java:24) 
        at android.app.Activity.performCreate(Activity.java:7183) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6944) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
Process 27039 terminated.

enter image description here

MainActivity.java ::MainActivity

package com.example.colorcomp2;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

Button cameraButton;
ImageView img;

final static int TAKE_PICTURE = 1001;   //requestCode를 위해서

// Used to load the 'native-lib' library on application startup.
static {
    System.loadLibrary("native-lib");
}

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

    cameraButton = findViewById(R.id.cameraButton);
    cameraButton.setOnClickListener(this);


    img = findViewById(R.id.imageView);
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.cameraButton:
            Intent camSurf = new Intent(getApplicationContext(), CameraActivity.class);
            startActivityForResult(camSurf,TAKE_PICTURE);
            break;
        }
    }
}

CameraActivity.java :: another Activity for SurfaceView

package com.example.colorcomp2;

import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.SurfaceHolder;

import androidx.appcompat.app.AppCompatActivity;

public class CameraActivity extends AppCompatActivity {

static CameraSurface cameraSurface;
SurfaceHolder surfaceHolder;

@Override
public void onCreate(Bundle saveInstanceState){
    super.onCreate(saveInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    getWindow().setFormat(PixelFormat.UNKNOWN);


    setContentView(R.layout.activity_camera); //error point
    cameraSurface = findViewById(R.id.camerasurface);
    surfaceHolder = cameraSurface.getHolder();
    surfaceHolder.addCallback(cameraSurface);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
}

CameraSurface.java :: SurfaceView that has a Filter(onDraw) Camera

package com.example.colorcomp2;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;



public class CameraSurface extends SurfaceView implements SurfaceHolder.Callback{

    Camera camera;
    boolean previewing = false;

    public CameraSurface(Context context) {
        super(context);
        camera = Camera.open();
    }

@Override
protected void onDraw(Canvas canvas){
    Paint p = new Paint();
    p.setStyle(Paint.Style.FILL_AND_STROKE);
    p.setColor(Color.GREEN);
    p.setTextSize(100);
    canvas.drawText("생활코딩 안녕하세요!", 20, 20, p);
}


@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    setWillNotDraw(false);
}


@Override
public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    if(previewing){
        camera.stopPreview();
        previewing = false;
    }

    if(camera != null){
        try{
            camera.setPreviewDisplay(holder);
            camera.startPreview();
            previewing = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }
}

activity_main.xml ::xml for MainActivity

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">


    <Button
        android:id="@+id/cameraButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""


        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.683" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="413dp"
        android:layout_height="282dp"
        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.207"
        tools:srcCompat="@tools:sample/avatars"

        />


</androidx.constraintlayout.widget.ConstraintLayout>

activity_camera.xml :: xml for CameraActivity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.colorcomp2.CameraSurface
        android:id ="@+id/camerasurface"
        android:layout_height="match_parent"
        android:layout_width="match_parent">
    </com.example.colorcomp2.CameraSurface>

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.colorcomp2">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- new Activity! -->
        <activity android:name=".CameraActivity" />
    </application>

    <uses-permission android:name="android.permission.CAMERA"></uses-permission>

</manifest>

I apologize in advance for the long passage. This is the only place I can hope for, because I've been up all night over this problem for three days and nothing is getting better.

I hope you all take care of your health and have a peaceful.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
J4BEZ
  • 374
  • 4
  • 17
  • Please post the complete stack trace as text, not as screenshots. – Mike M. Mar 11 '20 at 09:19
  • Thank you for your opinion. I will revise it and upload the stack trace soon! Sorry for the inconvenience. – J4BEZ Mar 11 '20 at 09:21
  • @MikeM. Stack trace has been modified to text! (I'll leave the picture pointing to the line that the source code wrote showed an error!) – J4BEZ Mar 11 '20 at 09:26
  • The `CAMERA` permission is a dangerous permission, and you need to request it at runtime on Marshmallow and above, if your `targetSdkVersion` is 23+, which is likely, these days: https://stackoverflow.com/q/32635704. For a quick test, you can manually enable that permission on your app's page in the device Settings, to make sure that's the cause of the current issue, before you dive into implementing all that permission code. – Mike M. Mar 11 '20 at 09:32
  • @MikeM. Wow...!! If Android 6.0 or higher or sdk version 23 or higher, we need to bring a little more safely permission!! Thank you for the great tip! during the method you gave me(Settings -> Apps -> Permission) I found that my App's Camera Permission was actually denied. When I allowed it, It became Work!! – J4BEZ Mar 11 '20 at 09:48
  • 1
    I think I can finally get a good night's sleep. Thank you so much. Have a nice day!!! >A – J4BEZ Mar 11 '20 at 09:49
  • No problem! Here's a similar question with some example code, to get you started: [ERROR: failed to connect to camera service @ Android marshmallow](https://stackoverflow.com/questions/37458157/error-failed-to-connect-to-camera-service-android-marshmallow) – Mike M. Mar 11 '20 at 09:53
  • 1
    Wow.. Thanks for your recommendation!!!! I'll modify my code!! Thank you very much!! – J4BEZ Mar 11 '20 at 09:59

0 Answers0