|
|
|
Android เกิดปัญหาทำไมเกิดอาการ ERROR ในการใส่ Code นับเวลาถอยหลัง |
|
|
|
|
|
|
|
สงสัยครับว่าทำไมผมใส่โค้ดนับเวลาถอยหลังแล้วเกิดการ ERROR ครับ
แล้วต้องแก้อย่างไรครับ
Code (Android-Java)
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class DrawView extends View
{
mTextField = (TextView)findViewById(R.id.showCount);
public int[] ball = new int[]
{
R.drawable.icebagg, R.drawable.icebagh, R.drawable.ball_green
};
public int[][] ball_position = new int[][]
{
{ 400, 50 }, { 500, 50 }, { 600, 50 }
};
public Bitmap bm[] = new Bitmap[ball.length];
public boolean[] goal = new boolean[ball.length];
public int screen_width, screen_height;
int goal_line = 200;
int ball_id = -1;
int score = 0;
public DrawView(Context context, Display display)
{
super(context);
this.setBackgroundResource(R.drawable.fa9);
screen_width = display.getWidth();
screen_height = display.getHeight();
for(int i = 0 ; i < ball.length ; i++)
{
bm[i] = BitmapFactory.decodeResource(getResources(), ball[i]);
goal[i] = false;
}
//นับถอยหลัง
new CountDownTimer(30000, 1000)
{
public void onTick(long millisUntilFinished)
{
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish()
{
mTextField.setText("done!");
}
}.start();
}
//นับถอยหลัง
protected void onDraw1(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.RED);
Paint circle = new Paint();
circle.setColor(Color.BLUE);
if (isPop){
canvas.drawCircle(100, 100, 40, circle);
}
invalidate();
}
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.rgb(0x3e, 0x9c, 0xbc));
p.setAntiAlias(true);
canvas.drawRect(0, screen_height - goal_line
, screen_width, screen_height, p);
p.setTextSize(50);
p.setColor(Color.WHITE);
canvas.drawText("Drag to Here", (screen_width / 2) - 140
, screen_height - (( goal_line / 2) - 10), p);
canvas.drawColor(0xFFFFFF);
canvas.drawText("ข้อความ", 123, 100, p);
p.setTextSize(70);
p.setColor(Color.rgb(0x3e, 0x9c, 0xbc));
canvas.drawText(String.valueOf(score), 50, 100, p);
for(int i = 0 ; i < ball.length ; i++) {
if(!goal[i]) {
canvas.drawBitmap(bm[i], ball_position[i][0] - (bm[i].getWidth() / 2)
, ball_position[i][1] - (bm[i].getHeight() / 2), null);
}
}
}
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction ) {
case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
ball_id = -1;
for(int i = 0 ; i < ball.length ; i++) {
double radCircle = Math.sqrt( Math.pow(Math.abs(X - ball_position[i][0]), 2)
+ Math.pow(Math.abs(Y - ball_position[i][1]), 2) );
if(radCircle < bm[i].getWidth() / 2 && !goal[i]) {
ball_id = i;
}
}
case MotionEvent.ACTION_MOVE:
if(ball_id != -1 && !goal[ball_id] ) {
ball_position[ball_id][0] = X;
ball_position[ball_id][1] = Y;
}
break;
case MotionEvent.ACTION_UP:
if(ball_id != -1 && !goal[ball_id] ) {
if(Y > screen_height - goal_line) {
goal[ball_id] = true;
score++;
}
}
break;
}
invalidate();
return true;
}
}
Tag : Mobile, Android
|
|
|
|
|
|
Date :
2013-01-23 20:35:50 |
By :
man |
View :
1239 |
Reply :
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Error อะไรครับ ? ลอง Capture LogCat มาให้ดูหน่อยครับ
|
|
|
|
|
Date :
2013-01-23 22:03:13 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แบบนี้อะครับ
|
|
|
|
|
Date :
2013-01-24 12:40:34 |
By :
man |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เหมือนกับว่าตำแหน่งของ Array มันเกิน Index ของ Array น่ะครับ อาจจะต้องใช้การ Debug ดูครับ
|
|
|
|
|
Date :
2013-01-24 13:29:01 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถามต่อครับ
Code (Android-Java)
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.content.Intent;
public class DrawView extends View
{
mTextField = (TextView)findViewById(R.id.showCount);
public int[] ball = new int[]
{
R.drawable.icebagg, R.drawable.icebagh, R.drawable.ball_green
};
public int[][] ball_position = new int[][]
{
{ 400, 50 }, { 500, 50 }, { 600, 50 }
};
public Bitmap bm[] = new Bitmap[ball.length];
public boolean[] goal = new boolean[ball.length];
public int screen_width, screen_height;
int goal_line = 200;
int ball_id = -1;
int score = 0;
public DrawView(Context context, Display display)
{
super(context);
this.setBackgroundResource(R.drawable.fa9);
screen_width = display.getWidth();
screen_height = display.getHeight();
for(int i = 0 ; i < ball.length ; i++)
{
bm[i] = BitmapFactory.decodeResource(getResources(), ball[i]);
goal[i] = false;
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.RED);
Paint circle = new Paint();
circle.setColor(Color.BLUE);
if (isPop){
canvas.drawCircle(100, 100, 40, circle);
}
invalidate();
}
protected void onDraw(Canvas canvas) {
for(int i = 0 ; i < ball.length ; i++) {
if(!goal[i]) {
canvas.drawBitmap(bm[i], ball_position[i][0] - (bm[i].getWidth() / 2)
, ball_position[i][1] - (bm[i].getHeight() / 2), null);
}
}
}
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction ) {
case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
ball_id = -1;
for(int i = 0 ; i < ball.length ; i++) {
double radCircle = Math.sqrt( Math.pow(Math.abs(X - ball_position[i][0]), 2)
+ Math.pow(Math.abs(Y - ball_position[i][1]), 2) );
if(radCircle < bm[i].getWidth() / 2 && !goal[i]) {
ball_id = i;
}
}
case MotionEvent.ACTION_MOVE:
if(ball_id != -1 && !goal[ball_id] ) {
ball_position[ball_id][0] = X;
ball_position[ball_id][1] = Y;
}
break;
case MotionEvent.ACTION_UP:
if(ball_id != -1 && !goal[ball_id] ) {
if(Y > screen_height - goal_line) {
goal[ball_id] = true;
score++;
Intent newActivity = new Intent(DrawView.this,Main1.class); <<<<<มัน ERROR ครับ เป็นเพราะอะไร
startActivity(newActivity); <<<<< มันมีเส้นสีแดงตลอดเลย
}
}
break;
}
invalidate();
return true;
}
}
|
|
|
|
|
Date :
2013-01-24 18:46:40 |
By :
man |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|