0

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:

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);
    }
wtsang02
  • 18,603
  • 10
  • 49
  • 67
user2586942
  • 73
  • 1
  • 10

1 Answers1

0

Your gameView object is currently null. If you have it in your XML layout file, you should instantiate it in onCreate with a line like this:

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.

Phil
  • 35,852
  • 23
  • 123
  • 164
  • thanks sir.sir i also want to know the maximum width and height of my view.How should i do that? – user2586942 Jul 18 '13 at 16:33
  • @user2586942 check out [this post](http://stackoverflow.com/questions/4074937/android-how-to-get-a-custom-views-height-and-width) – Phil Jul 18 '13 at 16:44