私たちは、私が使用しています私のアプリケーションでは、別のアクティビティ
に行くと配列データをリフレッシュする方法Z画像としてアルファベットとになってドラッグし、dropped.Iは問題array.Andでこれらの画像を保存しています戻るボタンをクリックするか、戻るボタンを押すと前のメニューに戻り、クリアボタンを押すと同じページにリダイレクトされ、最終的にはアルファベット画像が初期位置になります。 実際に私がクリアボタンを押したときに、同じ配列にイメージをオーバーライドすると、配列サイズが26にすぎないので、ArrayIndexOutOfBoundExceptionを取得しています。そのアクティビティがナビゲートされる前に、このビューが次に開かれると、アレイデータが新たに初期化されるように、別の画面に表示されます。
私は宣言して配列を定義したクラス1と上記のクラスビューにアクセスしている別のクラスを提供しています。また、別のアクティビティonClickに移動するボタンがあります。
DrawViewクラス:
public class DrawView extends View {
public ColorBall[] colorballs = new ColorBall[26]; // array that holds the balls
public int balID = 0; // variable to know what ball is being dragged
public DrawView(Context context) {
super(context);
setFocusable(true); //necessary for getting the touch events
// setting the start point for the balls
Point point1 = new Point();
point1.x = 50;
point1.y = 0;
Point point2 = new Point();
point2.x = 150;
point2.y = 0;
Point point3 = new Point();
point3.x = 250;
point3.y = 0;
Point point4 = new Point();
point4.x = 350;
point4.y = 0;
Point point5 = new Point();
point5.x = 400;
point5.y = 0;
Point point6 = new Point();
point6.x = 50;
point6.y = 50;
Point point7 = new Point();
point7.x = 150;
point7.y =50;
Point point8 = new Point();
point8.x = 250;
point8.y = 50;
Point point9 = new Point();
point9.x = 350;
point9.y = 50;
Point point10 = new Point();
point10.x = 400;
point10.y = 50;
Point point11 = new Point();
point11.x = 50;
point11.y = 100;
Point point12 = new Point();
point12.x = 150;
point12.y = 100;
Point point13 = new Point();
point13.x = 250;
point13.y = 100;
Point point14 = new Point();
point14.x = 350;
point14.y = 100;
Point point15 = new Point();
point15.x = 400;
point15.y = 100;
Point point16 = new Point();
point16.x = 50;
point16.y = 150;
Point point17 = new Point();
point17.x = 150;
point17.y = 150;
Point point18 = new Point();
point18.x = 250;
point18.y = 150;
Point point19 = new Point();
point19.x = 350;
point19.y = 150;
Point point20 = new Point();
point20.x = 400;
point20.y = 150;
Point point21 = new Point();
point21.x = 50;
point21.y = 200;
Point point22 = new Point();
point22.x = 150;
point22.y = 200;
Point point23 = new Point();
point23.x = 250;
point23.y = 200;
Point point24 = new Point();
point24.x = 350;
point24.y = 200;
Point point25 = new Point();
point25.x = 400;
point25.y = 200;
Point point26 = new Point();
point26.x = 200;
point26.y = 250;
// declare each ball with the ColorBall class
colorballs[0] = new ColorBall(context,R.drawable.e, point1);
colorballs[1] = new ColorBall(context,R.drawable.l, point2);
colorballs[2] = new ColorBall(context,R.drawable.z, point3);
colorballs[3] = new ColorBall(context,R.drawable.h, point4);
colorballs[4] = new ColorBall(context,R.drawable.a, point5);
colorballs[5] = new ColorBall(context,R.drawable.c, point6);
colorballs[6] = new ColorBall(context,R.drawable.y, point7);
colorballs[7] = new ColorBall(context,R.drawable.s, point8);
colorballs[8] = new ColorBall(context,R.drawable.b2, point9);
colorballs[9] = new ColorBall(context,R.drawable.o, point10);
colorballs[10] = new ColorBall(context,R.drawable.n, point11);
colorballs[11] = new ColorBall(context,R.drawable.v, point12);
colorballs[12] = new ColorBall(context,R.drawable.i, point13);
colorballs[13] = new ColorBall(context,R.drawable.x, point14);
colorballs[14] = new ColorBall(context,R.drawable.f, point15);
colorballs[15] = new ColorBall(context,R.drawable.j, point16);
colorballs[16] = new ColorBall(context,R.drawable.t, point17);
colorballs[17] = new ColorBall(context,R.drawable.q, point18);
colorballs[18] = new ColorBall(context,R.drawable.w, point19);
colorballs[19] = new ColorBall(context,R.drawable.u, point20);
colorballs[20] = new ColorBall(context,R.drawable.k, point21);
colorballs[21] = new ColorBall(context,R.drawable.r, point22);
colorballs[22] = new ColorBall(context,R.drawable.p, point23);
colorballs[23] = new ColorBall(context,R.drawable.d, point24);
colorballs[24] = new ColorBall(context,R.drawable.m, point25);
colorballs[25] = new ColorBall(context,R.drawable.g, point26);
}
// the method that draws the balls
@Override protected void onDraw(Canvas canvas) {
//canvas.drawColor(0xFFCCCCCC); //if you want another background color
//draw the balls on the canvas
for (ColorBall ball : colorballs) {
canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
}
}
// events when touching the screen
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
balID = 0;
for (ColorBall ball : colorballs) {
// check if inside the bounds of the ball (circle)
// get the center for the ball
int centerX = ball.getX() + 25;
int centerY = ball.getY() + 25;
// calculate the radius from the touch to the center of the ball
double radCircle = Math.sqrt((double) (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y)));
// if the radius is smaller then 23 (radius of a ball is 22), then it must be on the ball
if (radCircle < 23){
balID = ball.getID();
break;
}
// check all the bounds of the ball (square)
//if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){
// balID = ball.getID();
// break;
//}
}
break;
case MotionEvent.ACTION_MOVE: // touch drag with the ball
// move the balls the same as the finger
if (balID > 0) {
colorballs[balID-1].setX(X-25);
colorballs[balID-1].setY(Y-25);
}
break;
case MotionEvent.ACTION_UP:
// touch drop - just do things here after dropping
break;
}
// redraw the canvas
invalidate();
return true;
}
}
クラスQuiz1:
public class Quiz1 extends Activity{
LinearLayout lLayout;
TextView tView;
DrawView d;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.e);
ImageView v=(ImageView)findViewById(R.id.imageView1);
v.setScaleType(ImageView.ScaleType.FIT_XY);
ImageView v1=(ImageView)findViewById(R.id.imageView2);
v1.setAlpha(60);
ImageButton i3=(ImageButton)findViewById(R.id.imageButton3);
i3.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "'Home.'", Toast.LENGTH_SHORT).show();
Intent i=new Intent(Quiz1.this,LiamaLearningActivity.class);
startActivity(i);
}
});
ImageButton i2=(ImageButton)findViewById(R.id.imageButton2);
i2.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "'Cleared.'", Toast.LENGTH_SHORT).show();
Intent i=new Intent(Quiz1.this,Quiz1.class);
startActivity(i);
}});
ImageButton i1=(ImageButton)findViewById(R.id.imageButton1);
i1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "'Sample Quizzes.'", Toast.LENGTH_SHORT).show();
Intent i=new Intent(Quiz1.this,Quiz.class);
startActivity(i);
}});
lLayout = new LinearLayout(this);
lLayout.setOrientation(LinearLayout.VERTICAL);
//-1(LayoutParams.MATCH_PARENT) is fill_parent or match_parent since API level 8
//-2(LayoutParams.WRAP_CONTENT) is wrap_content
lLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
/* tView = new TextView(this);
tView.setText("Hello, This is a view created programmatically! " +
"You CANNOT change me that easily :-)");
tView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
lLayout.addView(tView);*/
d=new DrawView(this);
d.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, 700));
lLayout.addView(d);
AbsoluteLayout abs=(AbsoluteLayout)findViewById(R.id.a1);
abs.addView(lLayout);
//setContentView(lLayout);
super.onDestroy();
}
}
Logcat:あなたがしたい場合は[戻る]ボタンをクリックした時に
12-06 16:29:45.564: E/AndroidRuntime(278): java.lang.ArrayIndexOutOfBoundsException
12-06 16:29:45.564: E/AndroidRuntime(278): at com.avigma.learning.DrawView.onTouchEvent(DrawView.java:176)
12-06 16:29:45.564: E/AndroidRuntime(278): at android.view.View.dispatchTouchEvent(View.java:3766)
12-06 16:29:45.564: E/AndroidRuntime(278): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
12-06 16:29:45.564: E/AndroidRuntime(278): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
12-06 16:29:45.564: E/AndroidRuntime(278): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
12-06 16:29:45.564: E/AndroidRuntime(278): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
12-06 16:29:45.564: E/AndroidRuntime(278): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
12-06 16:29:45.564: E/AndroidRuntime(278): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
12-06 16:29:45.564: E/AndroidRuntime(278): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
12-06 16:29:45.564: E/AndroidRuntime(278): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
12-06 16:29:45.564: E/AndroidRuntime(278): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
12-06 16:29:45.564: E/AndroidRuntime(278): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
12-06 16:29:45.564: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 16:29:45.564: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123)
12-06 16:29:45.564: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-06 16:29:45.564: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 16:29:45.564: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
12-06 16:29:45.564: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-06 16:29:45.564: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-06 16:29:45.564: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method)
私が試したが、まだ問題が残っています。.. –