2011-10-29 11 views
11

Mam ten prosty układ:Android - Używanie niestandardowej SurfaceView w układzie 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" > 

      <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> 

ten jest zgodny, a aplikacja działa idealnie. Chciałem wymienić rodzajowe SurfaceView z mojego własnego niestandardowego 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 
    } 
} 

i użyć jej w xml układ:

<?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" /> 
. 
. 
. 

Jak tylko aktywność jest tworzony uzyskać wyjątek:

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 

Czy to nie jest dozwolone. Nie mogę znaleźć żadnych przykładów, aby to zrobić.

Edit:
Inne następnie roztwór podano poniżej, jego również wymagane, aby dodać wszystkie konstruktorów SurfaceView do 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 
} 
+0

Brakujący konstruktorzy łamali moją aplikację! Jak to rozgryzłeś? – Anshul

Odpowiedz

11

W układzie xml trzeba także napisać pakiet (gdzie zadeklarować swoją klasę) tak:

<com.your.package.here.PuzzleView 
        android:id="@+id/surfaceView1" 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:layout_weight = "1" /> 
+1

Dzięki. Zobacz także mt edit. – Yoav

0

chciałbym tworzyć prawdziwe rozwiązanie do innej odpowiedzi, ponieważ po raz pierwszy w ciągu przeczytać "Edytuj "sekcja w pierwotnym pytaniu.

Oto dwie rzeczy, które trzeba zrobić:

  1. Pierwsze (odpowiedź z @luksprog)

W układzie xml trzeba także napisać pakiet (gdzie można zadeklarować klasa) w następujący sposób:

<com.your.package.here.PuzzleView 
       android:id="@+id/surfaceView1" 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight = "1" /> 
  1. sekundę (Edycja z @Yoav)

Inne następnie roztwór podano poniżej, jest to również wymagane, aby dodać wszystkie konstruktorów z SurfaceView do 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 
} 
Powiązane problemy