您好,我正在制作一个拼图应用程序,为此我从一个高度 = 宽度 = 240 的大型位图制作了 16 个较小的位图。现在我必须一次在一个活动中动态显示这些位图。我尝试的代码无法正常工作,因为它在一行中显示所有图像并且图像尺寸太小。这是代码:
public void display()
{
float x =0;
float y = 0;
LinearLayout llMain = new LinearLayout(this);
for(int i=0;i<3;i++)
{
LinearLayout llRow = new LinearLayout(this);
for(int j=0;j<tiles.length/4;j++)
{
ImageView iv = new ImageView(this);
iv.setImageBitmap(tiles[j]);// get bitmap from image path
iv.setX(x);
iv.setY(y);
x = y = x + 60;
llRow.addView(iv);
}
llMain.addView(llRow);
}
setContentView(llMain);
}
}
这是LogCat:
01-05 00:32:35.820: D/dalvikvm(11346): GC_EXTERNAL_ALLOC freed 47K, 52% free 2599K/5379K, external 1990K/2108K, paused 135ms
01-05 00:32:44.183: D/dalvikvm(11346): GC_EXTERNAL_ALLOC freed 43K, 50% free 2739K/5379K, external 2477K/2527K, paused 18ms
01-05 00:32:45.156: I/dalvikvm(11346): Could not find method android.widget.ImageView.setX, referenced from method com.maju.jigsawpuzzle.PlayBoard.display
01-05 00:32:45.156: W/dalvikvm(11346): VFY: unable to resolve virtual method 3236: Landroid/widget/ImageView;.setX (F)V
01-05 00:32:45.156: D/dalvikvm(11346): VFY: replacing opcode 0x6e at 0x002e
01-05 00:32:45.156: D/dalvikvm(11346): VFY: dead code 0x0031-003e in Lcom/maju/jigsawpuzzle/PlayBoard;.display ()V
01-05 00:32:45.179: D/AndroidRuntime(11346): Shutting down VM
01-05 00:32:45.179: W/dalvikvm(11346): threadid=1: thread exiting with uncaught exception (group=0x40015578)
01-05 00:32:45.187: E/AndroidRuntime(11346): FATAL EXCEPTION: main
01-05 00:32:45.187: E/AndroidRuntime(11346): java.lang.NoSuchMethodError: android.widget.ImageView.setX
01-05 00:32:45.187: E/AndroidRuntime(11346): at com.maju.jigsawpuzzle.PlayBoard.display(PlayBoard.java:164)
01-05 00:32:45.187: E/AndroidRuntime(11346): at com.maju.jigsawpuzzle.PlayBoard.shuffleArray(PlayBoard.java:143)
01-05 00:32:45.187: E/AndroidRuntime(11346): at com.maju.jigsawpuzzle.PlayBoard.breakImage(PlayBoard.java:123)
01-05 00:32:45.187: E/AndroidRuntime(11346): at com.maju.jigsawpuzzle.PlayBoard.onCreate(PlayBoard.java:40)
01-05 00:32:45.187: E/AndroidRuntime(11346): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-05 00:32:45.187: E/AndroidRuntime(11346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
01-05 00:32:45.187: E/AndroidRuntime(11346): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-05 00:32:45.187: E/AndroidRuntime(11346): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-05 00:32:45.187: E/AndroidRuntime(11346): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-05 00:32:45.187: E/AndroidRuntime(11346): at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 00:32:45.187: E/AndroidRuntime(11346): at android.os.Looper.loop(Looper.java:123)
01-05 00:32:45.187: E/AndroidRuntime(11346): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-05 00:32:45.187: E/AndroidRuntime(11346): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 00:32:45.187: E/AndroidRuntime(11346): at java.lang.reflect.Method.invoke(Method.java:507)
01-05 00:32:45.187: E/AndroidRuntime(11346): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-05 00:32:45.187: E/AndroidRuntime(11346): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-05 00:32:45.187: E/AndroidRuntime(11346): at dalvik.system.NativeStart.main(Native Method)
第 164 行是以下行:
iv.setX(x);
有人知道解决方案吗?此外,我必须向他们注册 onClick ......
预先感谢