2012-03-25 70 views
-3

這裏是我創建的類的代碼,它擴展了MainActivity,我該如何從MainActivity調用它? 我想弄清楚我在引用我的表面視圖類時出錯的位置,而不是我的視圖。我只是以此爲例。這是我的主類:如何鏈接下面顯示的類與主要活動

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.View.OnTouchListener; 

public class SurfaceViewExample extends Activity implements OnTouchListener{ 

    OurView v; 
    Bitmap ball; 
    float x,y; 

    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     v=new OurView(this); 
     v.setOnTouchListener(this); 
     ball=BitmapFactory.decodeResource(getResources(),R.drawable.tennis_ball); 
     x = y = 0; 
     setContentView(v); 
    } 

    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     v.resume(); 
    } 
    public class OurView extends SurfaceView implements Runnable{ 
     Thread t; 

     SurfaceHolder holder; 

     boolean isItOk=false; 

    public OurView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     holder=getHolder(); 
    } 


    public void run() { 

     // TODO Auto-generated method stub 

     while(isItOk ==true) 
     { 
     //drawing 
     if(holder.getSurface().isValid()) { 

      continue; 

     } 

    Canvas c=holder.lockCanvas(); 
     c.drawARGB(255,150,150,10);  
     c.drawBitmap(ball, x+(ball.getWidth()/4), y+(ball.getHeight()), null); 

     holder.unlockCanvasAndPost(c);  

     } 
    } 
    public void pause() 
    { 
     isItOk=false; 
     while(true) { 
      try { 
       t.join(); 
      }catch(InterruptedException e) { 

       e.printStackTrace(); 

      } 
      break; 

     } 
    } 

    public void resume() 
    { 
     isItOk=true; 
     t=new Thread(this); 
     t.start(); 
    } 

    } 
    public boolean onTouch(View v, MotionEvent event) { 
    // TODO Auto-generated method stub 
    return false; 
     } 




    } 
+0

當你爲它使用'Intent'時會發生什麼? – sastraxi 2012-03-25 06:10:43

+0

如何使用意圖? – Madhukeshwara 2012-03-25 06:17:51

+1

'意圖'是你如何在Android中的'活動'之間導航。閱讀並嘗試瞭解http://developer.android.com/guide/topics/intents/intents-filters.html。如果您遇到問題,請在這裏提出一個關於您遇到的具體問題的新問題! – sastraxi 2012-03-25 06:19:08

回答

相關問題