代码之家  ›  专栏  ›  技术社区  ›  Niels Vanwingh

碎片相互作用

  •  0
  • Niels Vanwingh  · 技术社区  · 6 年前

    在我的主要活动中,我在布局中添加了两个片段。

    public class MainActivity extends AppCompatActivity {
    
    private Collapsebutton_Fragment cbut_frag;
    private ColourView_Fragment cvw_frag;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        cbut_frag = new Collapsebutton_Fragment();
        cvw_frag = new ColourView_Fragment();
    
        //ColourView Fragment ready
        //CollapseButton Fragment ready
    
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container_mainactivity, cbut_frag)
                .add(R.id.container_mainactivity, cvw_frag)
                .commit();
    }
    }
    

    public class ColourView_Fragment extends Fragment {
    
    FragmentManager fragmentManager;
    ThreeButton_Fragment tbt_fragment;
    public ColourView_Fragment(){}
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
        View rootView = inflater.inflate(R.layout.colourview_fragment, container, false);
    
        FrameLayout frameLayout = (FrameLayout)rootView.findViewById(R.id.ColourView);
    
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().beginTransaction()
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                        .add(R.id.container_mainactivity, tbt_fragment)
                        .commit();
            }
        });
    
        return rootView;
    }
    

    我得到以下错误:

    .add(R.id.container_mainactivity, tbt_fragment)
    

    我如何更正?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Abhijeet Kumar    6 年前

    您尚未初始化 tbt_fragment ColourView_Fragment