Programing

클래스를 확장할 때 팽창 오류

c10106 2022. 5. 21. 08:34
반응형

클래스를 확장할 때 팽창 오류

사용자 지정 보기를 만들려고 함GhostSurfaceCameraView그 연장선SurfaceView여기 내 클래스 정의 파일이 있다.

GhostSurfaceCameraView.java:

public class GhostSurfaceCameraView extends SurfaceView implements SurfaceHolder.Callback {
    SurfaceHolder mHolder;
    Camera mCamera;

    GhostSurfaceCameraView(Context context) {
        super(context);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where to draw.
        mCamera = Camera.open();
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException exception) {
            mCamera.release();
            mCamera = null;
            // TODO: add more exception handling logic here
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }   

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        parameters.set("orientation", "portrait");
        // parameters.setRotation(90); // API 5+
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }
}

그리고 이것은 내 ghostviewscreen.xml에 있다.

<com.alpenglow.androcap.GhostSurfaceCameraView android:id="@+id/ghostview_cameraview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"/>

이제 내가 한 활동에서:

protected void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ghostviewscreen);
    }
}

언제setContentView()호출되고 예외 발생:

Binary XML file 09-17 22:47:01.958: ERROR/ERROR(337):
ERROR IN CODE:
android.view.InflateException: Binary
XML file line #14: Error inflating
class
com.alpenglow.androcap.GhostSurfaceCameraView

내가 왜 이런 실수를 하는지 말해줄 사람 있어?고마워요.

왜 이게 안 되는지 알아낸 것 같아.나는 단지 하나의 매개 변수 '콘텍스트'의 경우에 대한 생성자를 제공하고 있었는데, 나는 두 매개 변수 '콘텍스트, 속성 집합'의 생성자를 제공했어야 했다.나는 또한 건설업자에게 공개적인 접근을 줄 필요가 있었다.여기 내 해결책이 있다.

public class GhostSurfaceCameraView extends SurfaceView implements SurfaceHolder.Callback {
        SurfaceHolder mHolder;
        Camera mCamera;

        public GhostSurfaceCameraView(Context context)
        {
            super(context);
            init();
        }
        public GhostSurfaceCameraView(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            init();
        }
        public GhostSurfaceCameraView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }

@Tim - 두 생성자가 모두 필요한 것은 아니며, 단지ViewClassName(Context context, AttributeSet attrs )건설자가 필요하다.나는 몇 시간이고 시간을 낭비한 후에 고통스러운 방법을 발견했다.

안드로이드 개발은 처음인데, 아마 풍습을 추가해서 그런 것 같아 추측이 난무하고 있다.ViewXML 파일의 클래스는 XML에 몇 가지 속성을 설정하는데, XML은 인스턴스화 시 처리해야 한다.그러나 나보다 훨씬 더 박식한 사람이 이 문제에 대해 더 명확하게 밝힐 수 있을 것이다.

"오류 팽창 클래스" 메시지의 또 다른 가능한 원인은 XML에 지정된 전체 패키지 이름의 철자가 잘못되었을 수 있다.

<com.alpenglow.androcap.GhostSurfaceCameraView android:id="@+id/ghostview_cameraview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

Eclipse XML 편집기에서 레이아웃 XML 파일을 열면 이 문제가 강조 표시된다.

xml에 전체 클래스 경로를 쓰는 것이 중요하다.서브클래스의 이름만 기재되어 있을 때 '오류 부풀리기 수업'을 받았다.

나는 지난 몇 시간 동안 이런 실수를 저질렀다.알고 보니, 나는 안드로이드 스튜디오에서 모듈로서 커스텀 뷰 리브를 추가했지만, 앱의 종속성으로 추가하는 것을 소홀히 했었다.build.gradle.

dependencies {
    ...
    compile project(':gifview')
}

fwww, 나는 null 객체에 접근하려는 생성자 내의 사용자 정의 초기화 때문에 이 오류를 수신했다.

나는 TextEdit를 확장하는데 같은 문제가 있었다.나에게 실수는 내가 건설업자에게 "공용"을 추가하지 않은 것이었다.내 경우에는 인수가 있는 생성자를 하나만 정의해도 작동한다.Context그리고AttributeSet. 유선상으로 연결된 것은 APK(노래 여부를 불문하고)를 구축해서 장치에 옮겨야 버그가 저절로 드러나게 된다는 것이다.애플리케이션이 AndroidStudio -> RunApp을 통해 USB에 연결된 기기에서 실행되면 앱이 작동한다.

나의 경우, 나는 그러한 순환 자원을 추가했다.

<drawable name="above_shadow">@drawable/above_shadow</drawable>

로 바뀌었다.

<drawable name="some_name">@drawable/other_name</drawable>

그리고 그것은 성공하였다.

는 다른 , <수고>라는을 알아채지 못했다.abstract수업. 추상 수업은 부풀릴 수 없다.

여기서 이해해야 할 점은 다음과 같다.

시스자ViewClassName(Context context, AttributeSet attrs )xml을 통해 customView를 부풀릴 때 호출된다.새 키워드를 사용하여 개체를 인스턴스화하지 않음(즉, 그렇지 않음)new GhostSurfaceCameraView()이렇게 하면 첫 번째 시공사라고 부르는 겁니다. public View (Context context).

반면 XML에서 뷰를 부풀릴 경우, 즉 사용 시setContentView(R.layout.ghostviewscreen);또는 사용findViewById,너,아니,너 말고! 안드로이드 시스템이 전화한다.ViewClassName(Context context, AttributeSet attrs )건설업자

이는 문서: "XML에서 뷰를 부풀릴 때 호출되는 구성자"를 읽을 때 명확하다. 참조: https://developer.android.com/reference/android/view/View.html#View(android.content.Context,%20android.util.AttributeSet)

따라서 기본 다형성을 절대 잊지 말고 설명서를 읽어라.그것은 많은 두통을 덜어준다.

참조URL: https://stackoverflow.com/questions/3739661/error-inflating-when-extending-a-class

반응형