简体   繁体   English

如何在活动的自定义视图中调用用户定义的方法

[英]how to call a user defined method in a custom view from an activity

i want to call the start method defined in Gameview class from the NewGame activity.basically i want to add onclicklistener and want to perform task specified inthe start() method whenever the button is clicked activity: 我想从NewGame activity调用Gameview类中定义的start方法。基本上,我想添加onclicklistener并想在每次单击按钮的活动中执行start()方法中指定的任务:

public class NewGame extends Activity implements OnClickListener {

GameView gameview;

@Override
public void onCreate (Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    //gameview=new GameView(this);
    setContentView(R.layout.activity_new_game);

    View startbutton=findViewById(R.id.start_button);

    startbutton.setOnClickListener(this);


}

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.start_button:
        gameview.start(this);
    }

}

view: 视图:

   public class GameView extends View {
    Path circle;
    Paint cPaint;
    Paint tPaint;
    String z;
    GameView a;
    int i=65,strt,arc,leftx,topy,rightx,bottomy,maxx,maxy,minx,miny;
    boolean flag1,flag2,flag3;
    double n1,n2;
    int n,n3=180,n4,n5=90;
    float f1=180,f2=90;
    int width;
    int height;



    Random r=new Random();


    RectF  oval;



    public GameView(Context context,AttributeSet attrs ) {

        super(context,attrs);

        leftx=0;
        topy=60;
        rightx=150;
        bottomy=120;

        z= String.valueOf(Character.toChars(i));

        cPaint = new Paint();
        cPaint.setColor(Color.RED);


        strt=45;
        arc=315;

        n1=Math.random()*600;
        Log.d("random",z);
        this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


        // cPaint.setStrokeWidth(2);

        tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        tPaint.setColor(Color.BLACK);
        float scale = getResources().getDisplayMetrics().scaledDensity;
        tPaint.setTextSize(20 * scale);
       }
    public void start(Context context)
    {
        if (flag2==false)
            new DrawThread(this);
    }

Your gameView object is currently null . 您的gameView对象当前为null If you have it in your XML layout file, you should instantiate it in onCreate with a line like this: 如果您的XML布局文件中有此文件,则应在onCreate使用以下行实例化它:

gameView = (GameView) findViewById(R.id.gameView);//where gameView is the id specified in your layout file (R.layout.main, or something)

If it is not in your layout file, you need to instantiate it and add it to your layout: 如果它不在布局文件中,则需要实例化它并将其添加到布局中:

gameView = new GameView(this);
gameView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
ViewGroup content = (ViewGroup) findViewById(android.R.id.content).getRootView();
content.addView(gameView);

now you will not get null pointer exceptions, and your gameview will fill the screen. 现在您将不会获得空指针异常,并且您的游戏视图将填满整个屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM