アクティビティからフラグメントを表示/非表示しようとしていますが、以下の例外があります。アクティビティからフラグメントのボタンを非表示/表示
android.view.ViewRootImpl $ CalledFromWrongThreadException:ビュー階層を作成した元のスレッドのみがそのビューに触れることができます。
ホーム活動
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);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCCCCC">
<TextView
android:id="@+id/list_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/shape_logo_bg"
android:gravity="center"
android:padding="5dp"
android:textColor="@android:color/white"
android:textSize="18sp" />
<Button
android:id="@+id/new_designs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/list_name"
android:background="@color/edit_button_color"
android:padding="10dp"
android:text="@string/new_designs"
android:textColor="@color/btn_text_color"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:visibility="gone"
/>
</RelativeLayout>
コードをここに掲載するには大きすぎます。なぜ私は問題を発見しているコードを投稿したのですか?
私はnewDesigns BUTTONインスタンスを取得できます。ボタンインスタンス(VISIBLE/GONE)で再生しようとすると、上記の例外が発生します。
お手数ですが、
同様の質問が何度も尋ねてきました!同様の質問をしないでください! –
このリンクを参照してくださいhttp://stackoverflow.com/questions/20124557/disable-button-in-fragment-from-main-activity –
[Android]ビューの階層を作成した元のスレッドのみがそのビューに触れることができます。 "フラグメントのエラー](http://stackoverflow.com/questions/18656813/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi) –