my APP should show and/or hide images according to some variables. Here's the code:
final ImageView greenSquare = (ImageView) findViewById(R.id.greensquare);
final ImageView greenBigSquare = (ImageView) findViewById(R.id.greenBIGsquare);
if (a) {
if (b) {
b = false;
a = false;
greenSquare.setVisibility(View.INVISIBLE);
greenBigSquare.setVisibility(View.VISIBLE);
} else {
b = true;
}
} else {
if (c) {
a = !a;
greenSquare.setVisibility(View.VISIBLE);
greenBigSquare.setVisibility(View.INVISIBLE);
}
}
And here's the XML
<ImageView
android:id="@+id/greenBIGsquare"
android:visibility="invisible"
opencv:srcCompat="@color/colorFollowMe" />
<ImageView
android:id="@+id/greensquare"
android:visibility="visible"
opencv:srcCompat="@color/colorAccent" />
but it doesn't run correctly and it crushes.
Here's the logcat
01-29 20:20:18.487 6535-6667/pinwheel.redblock E/AndroidRuntime: FATAL EXCEPTION: Thread-331 Process: pinwheel.redblock, PID: 6535 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6556) at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:942) at android.view.ViewGroup.invalidateChild(ViewGroup.java:5081) at android.view.View.invalidateInternal(View.java:12719) at android.view.View.invalidate(View.java:12683) at android.view.View.setFlags(View.java:10640) at android.view.View.setVisibility(View.java:7431) at android.widget.ImageView.setVisibility(ImageView.java:1469) at pinwheel.redblock.MainActivity_Show_camera.onCameraFrame(MainActivity_Show_camera.java:492) at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:391) at pinwheel.redblock.PortraitCameraView.access$400(PortraitCameraView.java:21) at pinwheel.redblock.PortraitCameraView$CameraWorker.run(PortraitCameraView.java:289) at java.lang.Thread.run(Thread.java:818)
Can anybody help me? Thank you
尝试使用@UiThread
注释方法
try this:
runOnUiThread(new Runnable() {
@Override
public void run() {
greenSquare.setVisibility(View.INVISIBLE);
greenBigSquare.setVisibility(View.VISIBLE);
}
});
also use this in your else block during changing the visibility..
any action involving the user interface must be done in the main or UI thread
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.