如何将下面显示的类与主要活动相关联

时间:2012-03-25 05:42:12

标签: java android eclipse

这是我创建的类的代码,它扩展了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;
      } 




  } 

1 个答案:

答案 0 :(得分:0)

我得到的很多就是你想要从你的主要活动中去SurfaceViewExample,因为你需要在点击按钮上使用意图

Intent i = new Intent(this, SurfaceViewExample.class);

startActivity(i) ;

并且您必须在清单中添加权限才能进行该活动,例如

<activity android:enabled="true" android:name="SurfaceViewExample" />




and you can see these links also

Calling one Activity from another in Android

http://developer.android.com/reference/android/content/Intent.html