如何向片段添加活动? [Android Studio]

我创建了一个带有底部导航菜单的应用程序。效果很好,但是当我尝试向其中一个片段添加活动(特别是向任务部分片段添加新任务的活动)时,代码似乎运行良好,但是当我运行程序并单击按钮以添加新任务将停止整个应用程序。

button error

这是我的主要活动的代码:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    HomeFragment fragment1 = new HomeFragment();
    FragmentManager homemanager = getSupportFragmentManager();
    homemanager.beginTransaction().add(R.id.fragment_container, fragment1).commit();


    BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
    bottomNav.setOnNavigationItemSelectedListener(navListener);

    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
            new HomeFragment()).commit();
}

private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()) {
                    case R.id.nav_home:
                        selectedFragment = new HomeFragment();
                        break;
                    case R.id.nav_calendar:
                        selectedFragment = new CalendarFragment();
                        break;
                    case R.id.nav_school:
                        selectedFragment = new SchoolFragment();
                        break;
                }

                FragmentManager navmanager = getSupportFragmentManager();
                navmanager.beginTransaction().replace(R.id.fragment_container,
                        selectedFragment).commit();

                return true;
            }
        };

}

这是我的家庭片段代码:

public class HomeFragment extends Fragment {

Button addNewBtn;

public HomeFragment(){
    //Required empty constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_home, container, false);

    addNewBtn = v.findViewById(R.id.btnAddNew);
    addNewBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            startActivity(new Intent(getActivity(),NewTaskHome.class));
        }
    });

    return v;
}

} }

我是Android Studio的新手,非常欢迎您的帮助。

我不明白为什么会收到此错误?

评论