gpt4 book ai didi

java - ImageView 只能工作一次

转载 作者:行者123 更新时间:2023-12-01 12:58:09 25 4
gpt4 key购买 nike

我的 XML 中有 9 个 ImageView。

在类中声明ImageView:

ImageView image;

关于代码的一些信息:这是一个井字棋游戏,用户与智能手机,当我用户单击任何 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)

日志猫:

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)

最佳答案

看来您正在 TimerTask 内运行计算机播放代码。它不在 UI 线程上运行,因此会抛出您在尝试修改 View 时看到的错误。

在您的 TimerTask 中,使用以下代码来包装您的现有代码:

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

关于java - ImageView 只能工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23729264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com