Dave Moore
Dave Moore

Reputation: 1452

SurfaceView failed to instantiate

I'm trying to implement a custom SurfaceView in my activity's XML file, and then reference it within the activity's code for drawing etc. However, I'm getting the following error:

The following classes could not be instantiated:
- com.example.animateddrawable.MainActivity.DrawingPanel 

MainActivity Code:

package com.example.animateddrawable;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
...


com.example.animateddrawable.MainActivity.DrawingPanel.PanelThread;


public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dp = new DrawingPanel(this, null);
    setContentView(R.layout.activity_main);

    }

    public class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
        public DrawingPanel( Context context, AttributeSet attributeSet){
        super(context, attributeSet);
        init();
    }

    public void init(){
        getHolder().addCallback(this);
    }

    }
}

XML Code

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <com.example.animateddrawable.MainActivity.DrawingPanel
            android:id="@+id/surfaceView"
            android:layout_width = "fill_parent"
            android:layout_height = "fill_parent"/>

    </FrameLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</LinearLayout>

Any ideas?

Upvotes: 0

Views: 486

Answers (1)

Dyna
Dyna

Reputation: 2305

try to add a constructor like this:

public DrawingPanel(Context context) {
    super(context);
    getHolder().addCallback(this);
}
public DrawingPanel(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    getHolder().addCallback(this);
}

add this to your other constructer as well:

 getHolder().addCallback(this);

Upvotes: 1

Related Questions