I have this simple layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight = "1" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2" />
</LinearLayout>
</LinearLayout>
This complies and the application runs perfectly. I wanted to replace the generic SurfaceView with my own custom SurfaceView:
import android.content.Context;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class PuzzleView extends SurfaceView implements SurfaceHolder.Callback {
public PuzzleView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
And use that in the layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<PuzzleView
android:id="@+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight = "1" />
.
.
.
As soon as the activity is created I get an exception:
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
}
10-29 19:56:25.921: E/AndroidRuntime(287): java.lang.RuntimeException: Unable to start activity ComponentInfo{ybz.pack1/ybz.pack1.MyActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class PuzzleView
Is this not allowed. I can't find any examples for doing this.
Edit:
Other then the solution given below, its also required to add all the constructors of SurfaceView to PuzzleView:
public PuzzleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public PuzzleView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
In your xml layout you also have to write the package(where you declare your class) like this:
<com.your.package.here.PuzzleView
android:id="@+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight = "1" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With