Android 在活动的片段中隐藏/显示按钮
我试图从活动中隐藏/显示片段中的按钮,但它给了我以下异常 android.view.ViewRootImpl$CalledFromErrorThreadException:只有创建视图层次结构的原始线程才能接触其视图 家庭活动Android 在活动的片段中隐藏/显示按钮,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,我试图从活动中隐藏/显示片段中的按钮,但它给了我以下异常 android.view.ViewRootImpl$CalledFromErrorThreadException:只有创建视图层次结构的原始线程才能接触其视图 家庭活动 public class HomeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { s
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
CategoryFragment frag=(CategoryFragment) activity.getSupportFragmentManager()
.findFragmentByTag("cat_frag");
Button newDesigns= (Button) frag.getView().findViewById(R.id.new_designs);
newDesigns.setVisibility(View.VISIBLE);
}
}
类别片段
public class CategoryFragment extends Fragment{
Button newDesigns;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_category, null);
newDesigns= (Button) v.findViewById(R.id.new_designs);
}
}
代码太大,无法在此处发布。这就是为什么我只在发现问题的地方发布代码
我能够获得newDesigns按钮实例。让我震惊的是,如果尝试使用按钮实例(可见/消失),它会给我上述异常
非常感谢您的帮助。在fragment类中添加一个方法
public void changeButtonVisibility(boolean visibility){
if(visibility){
newDesigns.setVisibility(View.VISIBLE);
}else{
newDesigns.setVisibilty(View.GONE);
}
}
在你的活动课上
添加这一行
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
CategoryFragment frag=(CategoryFragment) activity.getSupportFragmentManager()
.findFragmentByTag("cat_frag");
frag.changeButtonVisibility(true);
}
}
当您直接在活动中时,不应使用片段视图。您将不知道视图的状态是什么,这可能会导致您甚至想不起来的问题(请相信我,我遇到了许多问题)。要与活动视图交互,请创建一个界面:
public interface AccessFragmentViews{
public void setVisibilityForButton(boolean bool);
//any other methods that you need
}
现在在fragment类中实现它并重写该方法
class YourFragment implements AccessFragmentViews{
.
.
public void serVisibilityForButton(boolean shouldHide){
if(shouldHide){
yourButton.setVisibility(View.GONE);
}else{
yourButton.setVisibility(View.VISIBLE);
}
}
}
现在,您可以使用此接口与活动中的片段视图进行安全交互。但在执行此操作之前,请确保片段处于活动状态;)访问子视图容易出现WindowLeakedException和IllegalStateException
在活动中,按如下方式使用:
您可以通过通过标记查找片段引用,或者使用用于创建片段的引用来获取片段引用
//注意:访问活动中的片段视图是非常危险的
//first the alive check then the logic
if(yourFragmentReference!=null){
((AccessFragmentViews)yourFragmentReference).setVisibilityForButton(true);// or false if you want to make it visible
}
类似的问题已经被问过很多次了!不要问类似的问题!请参阅此链接此链接的可能副本将在不需要时保留不必要的引用。此外,如果片段被破坏,应用程序将崩溃。这只是一个例子,有几种访问活动片段的方法,如使用标记引用的
,使用弱引用的,使用变量引用的,对于这个特定的例子,这个方法最适合。他想更改片段视图,就我理解他的问题而言,不是活动观点。。。颠倒顺序,然后lol。@MohammedAtif谢谢:)我猜,颠倒顺序的实现稍微有点棘手,您需要通过片段的标记访问片段来调用方法。当然,接口在您的情况下是可以工作的,但是反向操作没有什么危险。@MohammedAtif如果两者都是危险的话。我检查了他拥有的一个片段引用,但这并不能使他免于所有问题。他想要这个,我还能做什么呢
//first the alive check then the logic
if(yourFragmentReference!=null){
((AccessFragmentViews)yourFragmentReference).setVisibilityForButton(true);// or false if you want to make it visible
}