K
krackmoe
Neues Mitglied
- 1
Ich hab eine Klasse die Activity extended und eine die View extended.
In der die View extended habe ich eine onDraw Methode, und in dieser eine setOnTouchListener.
Dort möchte ich einfach x und y Koordinaten abfragen, und wenn diese dem entsprechen was ich haben will, soll er dem LinearLayout per addView einen Kreis reinzeichnen lassen.
Ich bekomm aber immer beim addView eine NPE, versteh aber nicht warum!
Maybe könnt ihr mir helfen, wäre super.
View Klasse:
Circle
Activity Klasse
In der die View extended habe ich eine onDraw Methode, und in dieser eine setOnTouchListener.
Dort möchte ich einfach x und y Koordinaten abfragen, und wenn diese dem entsprechen was ich haben will, soll er dem LinearLayout per addView einen Kreis reinzeichnen lassen.
Ich bekomm aber immer beim addView eine NPE, versteh aber nicht warum!
Maybe könnt ihr mir helfen, wäre super.
View Klasse:
Code:
package com.tictactoe;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
public class GameWindow extends View {
private Paint mLine;
private float mTouchX;
private float mTouchY;
private int x1TopBottom;
private int x2TopBottom;
private int y1LeftRight;
private int y2LeftRight;
private int xRight;
private int yBottom;
private LinearLayout parent;
public GameWindow(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getMeasuredWidth();
int height = getMeasuredHeight();
mLine = new Paint();
mLine.setColor(Color.BLACK);
mLine.setStrokeWidth(3);
x1TopBottom = width/3;
x2TopBottom = x1TopBottom*2;
int yTop = 0;
int xLeft = 0;
y1LeftRight = height/3;
y2LeftRight = y1LeftRight*2;
xRight = width;
yBottom = height;
canvas.drawLine(x1TopBottom, yTop+20, x1TopBottom, yBottom-20, mLine);
canvas.drawLine(x2TopBottom, yTop+20, x2TopBottom, yBottom-20, mLine);
canvas.drawLine(xLeft+20, y1LeftRight, xRight-20, y1LeftRight, mLine);
canvas.drawLine(xLeft+20, y2LeftRight, xRight-20, y2LeftRight, mLine);
parent.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
mTouchX = event.getX();
mTouchY = event.getY();
LinearLayout linearL = (LinearLayout)v;
if(mTouchX >= 0 && mTouchX <= x1TopBottom && mTouchY <= y1LeftRight && mTouchY >= 0){
try{
linearL.addView(new Circle(GameWindow.this.getContext(), mTouchX, mTouchY, 25));
}
catch(Exception e){
Log.v("tictactoe"," "+e);
}
}
}
return false;
}
});
}
public void setParentView(LinearLayout linearLayout) {
this.parent = linearLayout;
}
}
Circle
Code:
package com.tictactoe;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
public class Circle extends View {
private float x;
private float y;
private int r;
private Paint mPaint;
public Circle(Context context, float x, float y, int r){
super(context);
this.x = x;
this.y = y;
this.r = r;
mPaint.setColor(Color.RED);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(x,y,r, mPaint);
}
}
Activity Klasse
Code:
package com.tictactoe;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class GameActivity extends Activity {
private GameWindow gameWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_window);
gameWindow = (GameWindow)findViewById(R.id.game_window);
gameWindow.setParentView((LinearLayout)findViewById(R.id.linearL));
gameWindow.setFocusable(true);
gameWindow.setFocusableInTouchMode(true);
}
}