2011-06-21 50 views
1

我創建了一個類是這樣的:我可以在SurfaceView中製作按鈕嗎?

public class nir extends Activity implements OnClickListener{ 

    /** Called when the activity is first created. */ 
    ImageButton btnNew, btnPlay, btnSignUp; 
    TextView txtTitle; 
    Typeface font; 
    //sprite 
    SurfaceView pet; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     font = Typeface.createFromAsset(getAssets(), "fonts/aaaiight.ttf"); 
     btnNew = (ImageButton)findViewById(R.id.btnNew); 
     btnNew.setOnClickListener(this); 

     btnPlay = (ImageButton)findViewById(R.id.btnPlay); 
     btnPlay.setOnClickListener(this); 

     txtTitle = (TextView)findViewById(R.id.lblTama); 
     txtTitle.setTypeface(font); 

     //sprite 

     //setContentView(new MainGamePanel(this)); 

    } 

} 

現在我要做出SurfaceView顯示任何精靈。我試過

<view class="Tamagotchi.nir.MainGamePanel..."> 

在我的XML中,但它給了我一個「強制關閉」對話框。

我也有這個類:

public class MainGamePanel extends SurfaceView implements 
     SurfaceHolder.Callback { 

    private static final String TAG = MainGamePanel.class.getSimpleName(); 

    private MainThread thread; 
    private Sprite elaine; 

    // the fps to be displayed 
    private String avgFps; 
    public void setAvgFps(String avgFps) { 
     this.avgFps = avgFps; 
    } 

    public MainGamePanel(Context context) { 
     super(context); 
     // adding the callback (this) to the surface holder to intercept events 
     getHolder().addCallback(this); 

     // create Elaine and load bitmap 
     elaine = new Sprite(
      BitmapFactory.decodeResource(getResources(), 
          R.drawable.walk_elaine), 
          10, 50, // initial position 
      30, 47, // width and height of sprite 
      5, 5); // FPS and number of frames in the animation 

     // create the game loop thread 
     thread = new MainThread(getHolder(), this); 

     // make the GamePanel focusable so it can handle events 
     setFocusable(true); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     // at this point the surface is created and 
     // we can safely start the game loop 
     thread.setRunning(true); 
     thread.start(); 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     Log.d(TAG, "Surface is being destroyed"); 
     // tell the thread to shut down and wait for it to finish 
     // this is a clean shutdown 
     boolean retry = true; 
     while (retry) { 
      try { 
       thread.join(); 
       retry = false; 
      } catch (InterruptedException e) { 
       // try again shutting down the thread 
      } 
     } 
     Log.d(TAG, "Thread was shut down cleanly"); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      // handle touch 
     } 
     return true; 
    } 

    public void render(Canvas canvas) { 
     canvas.drawColor(Color.BLACK); 
     elaine.draw(canvas); 
     // display fps 
     displayFps(canvas, avgFps); 
    } 

    /** 
    * This is the game update method. It iterates through all the objects 
    * and calls their update method if they have one or calls specific 
    * engine's update method. 
    */ 
    public void update() { 
     elaine.update(System.currentTimeMillis()); 
    } 

    private void displayFps(Canvas canvas, String fps) { 
     if (canvas != null && fps != null) { 
      Paint paint = new Paint(); 
      paint.setARGB(255, 255, 255, 255); 
      canvas.drawText(fps, this.getWidth() - 50, 20, paint); 

     } 
    } 
} 

什麼是崩潰的原因是什麼?

+1

怎麼樣logcat的marol? – Deva

+0

什麼是logcat?你能告訴我如何使用它嗎? – marol

+0

@octavian damiean:我不知道太....因爲我不知道,我問一個專家在那裏...... – marol

回答

0

取出view class = 在你的包名稱前所有你需要的是包name.YourSurfaceViewClass

這是你的代碼應該是什麼樣子

<Tamagotchi.nir.MainGamePanel.YourSurfaceViewClassName 
android:id="@+id/myView" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/> 
+0

如果我讀到你的問題是正確的答案 – rdawg71481

相關問題