ImageView不会多次使用

时间:2014-05-19 03:23:31

标签: java android imageview drawable

我的XML中有9个ImageView。

在类中声明了ImageView:

ImageView image;

关于代码的一点点: 这是一个tic tac toe游戏,用户与智能手机,当我用户点击任何ImageView时,图像变化很好,我希望通过onClick(View v)功能使用:

        image = (ImageView) findViewById(R.id.block2);
        image.setImageResource(R.drawable.block_2_x);

在我的回合结束后,智能手机正在播放,使用相同的两行但具有不同的ID和DRAWABLE。 调用另一个名为computerPlay() { ... }

的函数

然后没有任何事情发生,但错误。

但是,当我再次运行应用程序并且智能手机首先启动时,图像在computerPlay() { .. }

中变化很好

然后轮到我了...

不知何故,我觉得这是因为我不能多次使用相同的ImageView?


代码的大图: (当用户点击图片时)

public void touch(View v) {

    ........
                image = (ImageView) findViewById(R.id.block1);
                image.setImageResource(R.drawable.block_1_x);
.......
}

计算机玩的时候:

public void changeImage() {
...
                    image = (ImageView) findViewById(R.id.block2);
                    image.setImageResource(R.drawable.block_1_o);
...
}

任何想法?


错误:

java.lang.IllegalArgumentException: Widget has the wrong parent
    at org.eclipse.swt.SWT.error(SWT.java:4342)
    at org.eclipse.swt.SWT.error(SWT.java:4276)
    at org.eclipse.swt.SWT.error(SWT.java:4247)
    at org.eclipse.swt.widgets.Widget.error(Widget.java:468)
    at org.eclipse.swt.widgets.Control.setMenu(Control.java:3462)
    at com.android.ddmuilib.logcat.LogCatPanel$20.handleEvent(LogCatPanel.java:1042)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
    at org.eclipse.swt.widgets.Widget.showMenu(Widget.java:1472)
    at org.eclipse.swt.widgets.Widget.wmContextMenu(Widget.java:1574)
    at org.eclipse.swt.widgets.Control.WM_CONTEXTMENU(Control.java:4673)
    at org.eclipse.swt.widgets.Table.WM_CONTEXTMENU(Table.java:5972)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4532)
    at org.eclipse.swt.widgets.Table.windowProc(Table.java:5913)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:4989)
    at org.eclipse.swt.internal.win32.OS.CallWindowProcW(Native Method)
    at org.eclipse.swt.internal.win32.OS.CallWindowProc(OS.java:2440)
    at org.eclipse.swt.widgets.Table.callWindowProc(Table.java:564)
    at org.eclipse.swt.widgets.Table.callWindowProc(Table.java:430)
    at org.eclipse.swt.widgets.Widget.wmRButtonUp(Widget.java:2395)
    at org.eclipse.swt.widgets.Control.WM_RBUTTONUP(Control.java:5134)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4596)
    at org.eclipse.swt.widgets.Table.windowProc(Table.java:5913)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:4989)
    at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
    at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2546)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3756)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1053)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1414)

logcat的:

05-18 23:43:25.760: E/AndroidRuntime(1377): FATAL EXCEPTION: Timer-0
05-18 23:43:25.760: E/AndroidRuntime(1377): Process: com.example.tttgame, PID: 1377
05-18 23:43:25.760: E/AndroidRuntime(1377): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
05-18 23:43:25.760: E/AndroidRuntime(1377):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6006)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:822)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at android.view.View.requestLayout(View.java:16392)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at android.view.View.requestLayout(View.java:16392)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at android.view.View.requestLayout(View.java:16392)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at android.view.View.requestLayout(View.java:16392)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at android.view.View.requestLayout(View.java:16392)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at android.view.View.requestLayout(View.java:16392)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at android.view.View.requestLayout(View.java:16392)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at android.view.View.requestLayout(View.java:16392)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at android.widget.ImageView.setImageResource(ImageView.java:370)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at com.example.tttgame.BoardActivity.changeImage(BoardActivity.java:755)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at com.example.tttgame.BoardActivity.fillboard(BoardActivity.java:739)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at com.example.tttgame.BoardActivity.randPick(BoardActivity.java:715)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at com.example.tttgame.BoardActivity.computerPlay(BoardActivity.java:525)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at com.example.tttgame.BoardActivity$7.run(BoardActivity.java:387)
05-18 23:43:25.760: E/AndroidRuntime(1377):     at java.util.Timer$TimerImpl.run(Timer.java:284)

1 个答案:

答案 0 :(得分:1)

您似乎正在TimerTask内运行计算机播放代码。它不在UI线程上运行,因此会引发您在尝试修改视图时看到的错误。

TimerTask内,使用以下代码打包现有代码:

runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // This will run on the UI thread
            changeImage();
        }
    });