티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
Error inflating when extending a class
클래스를 상속하였을 때 인플레이트 오류
문제 내용
I'm trying to create a custom view GhostSurfaceCameraView
that extends SurfaceView
. Here's my class definition file
저는 SurfaceView를 상속하는 사용자 정의 뷰 GhostSurfaceCameraView를 만들고 있습니다. 여기에 제 클래스 정의 파일이 있습니다.
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();
}
}
And this is in my ghostviewscreen.xml:
그리고 이것은 제 ghostviewscreen.xml에 있습니다:
<com.alpenglow.androcap.GhostSurfaceCameraView android:id="@+id/ghostview_cameraview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Now in the activity I made:
이제 제가 만든 액티비티에서:
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.ghostviewscreen);
}
}
When setContentView()
gets called, an exception is thrown:
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
Can anyone tell me why I get this error? Thanks.
어떤 이유로 이 오류가 발생하는지 알려주실 분 계신가요? 감사합니다.
높은 점수를 받은 Solution
I think I figured out why this wasn't working. I was only providing a constructor for the case of one parameter 'context' when I should have provided a constructor for the two parameter 'Context, AttributeSet' case. I also needed to give the constructor(s) public access. Here's my fix:
저는 하나의 매개 변수 'context' 경우에 대한 생성자만 제공하고 있었기 때문에 이게 작동하지 않았던 것 같다는 것을 알아냈습니다. 대신 두 개의 매개 변수 'Context, AttributeSet'에 대한 생성자를 제공해야 했습니다. 또한 생성자에 대한 public 접근 권한을 부여해야 합니다. 여기 제가 고친 부분입니다:
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();
}
가장 최근 달린 Solution
Another possible cause of the "Error inflating class" message could be misspelling the full package name where it's specified in XML:
"Error inflating class" 메시지의 다른 가능한 원인은 XML에서 전체 패키지 이름의 철자가 틀렸을 때입니다:
<com.alpenglow.androcap.GhostSurfaceCameraView android:id="@+id/ghostview_cameraview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Opening your layout XML file in the Eclipse XML editor should highlight this problem.
Eclipse XML 편집기에서 레이아웃 XML 파일을 열면 이 문제가 강조될 것입니다.
출처 : https://stackoverflow.com/questions/3739661/error-inflating-when-extending-a-class
'개발 > 안드로이드' 카테고리의 다른 글
NotificationCompat.Builder 제대로 사용하기 (0) | 2023.02.08 |
---|---|
리스트뷰에서 ArrayIndexOutOfBoundsException 오류 수정하기 (0) | 2023.02.08 |
Environment.getExternalStorageDirectory() 대신 내부 저장소 디렉토리 가져오기 (0) | 2023.02.07 |
부모 액티비티로 돌아가기 (0) | 2023.02.06 |
'java.net.SocketException: socket failed: EACCES (Permission denied)' 수정하기 (0) | 2023.02.06 |