我在布局中添加了一个片段,其中包含一个Web视图,并且可以正常工作。现在,我尝试在代码的javascript中触发动作以隐藏片段本身。
我实现了一个名为WebAppInterface的类,可以从Javascript调用该类,如果按下按钮,则会调用hidePicker()。现在,我尝试隐藏片段,但是什么也没有发生。这是我的功能:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class WebAppInterface extends FragmentActivity{
public void hidePicker(){
Log.d("CAPerm", "hide Picker");
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out);
Fragment picker = fm.findFragmentById(R.id.registrationpickerfragment);
ft.hide(picker);
ft.commit();
Log.d("CAPerm", "hidden");
}
}
该片段显示在ConstraintLayout的Scrollview的LinearLayout中。
我收到以下控制台输出:
W/System.err: java.lang.IllegalStateException: Activity has been destroyed
W/System.err: at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2087)
W/System.err: at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:678)
W/System.err: at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:632)
at com.mypackage.WebAppInterface.hidePicker(WebAppInterface.java:43)
at android.os.MessageQueue.nativePollOnce(Native Method)
W/System.err: at android.os.MessageQueue.next(MessageQueue.java:386)
at android.os.Looper.loop(Looper.java:169)
at android.os.HandlerThread.run(HandlerThread.java:65)
V/WindowManager: Exit animation finished in Window{fe0401a u0 com.mypackage.UserRegistration EXITING}: remove=true
E/WindowManager: win=Window{fe0401a u0 com.mypackage.UserRegistration EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true
V/WindowManager: postWindowRemoveCleanupLocked: Window{fe0401a u0 com.mypackage.UserRegistration}
Removing Window{fe0401a u0 com.mypackage.UserRegistration} from AppWindowToken{b12e137 token=Token{266fe36 ActivityRecord{37b83d1 u0 com.mypackage.UserRegistration t6381}}}
更高版本的日志消息“ hidden”没有执行,并且该错误似乎在第43行(ft.commit())发生了
有什么想法为什么不删除该片段或一些解决方法如何更好地解决它?
预先感谢:)
关于基督徒
答案 0 :(得分:0)
我终于可以解决我的问题了:)
我在UI线程中创建了一个带有事件侦听器的布尔变量,如下所示:Android: Listening to variable changes - Stack Overflow,并在WebAppInterface函数中调用了setBoo:)