我试图将我的活动转换为一个片段,但出现了一些问题。homefragment中的布局为空,因此发生崩溃。。。
我的家庭活动:
public class HomeActivity extends AppCompatActivity {
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//the layout on which you are working
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.homeContainer);
final ConstraintLayout layout2 = (ConstraintLayout) findViewById(R.id.homeContainer);
ViewTreeObserver vto = layout2.getViewTreeObserver();
vto.addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
...
我的家庭片段:
public class HomeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_home, container, false);
//the layout on which you are working
ConstraintLayout layout = (ConstraintLayout) getActivity().findViewById(R.id.homeContainer);
final ConstraintLayout layout2 = (ConstraintLayout) getActivity().findViewById(R.id.homeContainer);
ViewTreeObserver vto = layout2.getViewTreeObserver();
vto.addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
...
有什么想法吗?
1条答案
按热度按时间vs91vp4v1#
你应该换掉你的衣服
getActivity()
用你夸张的观点打电话。之前
之后