【问题标题】:Android Studio NullPointerException caused by fragmentTransaction由 fragmentTransaction 引起的 Android Studio NullPointerException
【发布时间】:2023-04-02 07:41:01
【问题描述】:

我的应用在添加片段后停止工作。特别是 MainActivity.java 中的这个函数:

 public void setFragment (Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.framelayout,fragment);
        fragmentTransaction.commit();
    }    

然后在 onCreate 内部调用:

setFragment(newsFeedFragment);

newsFeedFragmentMainActivity 类中定义:

NewsFeedFragment newsFeedFragment;

这是 NewsFeedFragment

的定义
public class NewsFeedFragment extends Fragment {
    Context context;
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.context=context;
    }
    // Here we are going to return our layout view
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_newsfeed,container,false);

        return view;
    }
}

应用程序崩溃,我收到此错误:

虽然很明显是函数导致了这个错误,但我不知道究竟是哪个部分导致了这个错误。
以防万一,这是整个 MainActivity.java

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.search)
    ImageView search;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.framelayout)
    FrameLayout framelayout;
    @BindView(R.id.fab)
    FloatingActionButton fab;
    @BindView(R.id.bottom_navigation)
    BottomNavigationView bottomNavigation;

    NewsFeedFragment newsFeedFragment;
    NotificationFragment notificationFragment;
    FriendsFragment friendsFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        bottomNavigation.inflateMenu(R.menu.bottom_navigation_main);
        bottomNavigation.setItemBackgroundResource(R.color.colorPrimary);
        bottomNavigation.setItemTextColor(ContextCompat.getColorStateList(bottomNavigation.getContext(),R.color.nav_item_colors));
        bottomNavigation.setItemIconTintList(ContextCompat.getColorStateList(bottomNavigation.getContext(),R.color.nav_item_colors));

        setFragment(newsFeedFragment);
        bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener(){
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.newsfeed_fragment:
                        setFragment(newsFeedFragment);
                        break;

                    case R.id.profile_fragment:

                        break;

                    case R.id.profile_friends:
                        setFragment(friendsFragment);
                        break;

                    case R.id.profile_notification:
                        setFragment(notificationFragment);
                        break;
                }
                return true;
            }
        });
    }

    public void setFragment (Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.framelayout,fragment);
        fragmentTransaction.commit();
    }
}

【问题讨论】:

    标签: java android nullpointerexception


    【解决方案1】:
      newsFeedFragment = new NewsFeedFragment();
      setFragment(newsFeedFragment);
    

    【讨论】:

      【解决方案2】:

      因为newsFeedFragmentnull, 在使用之前初始化你的片段。

      newsFeedFragment=NewsFeedFragment();
      setFragment(newsFeedFragment);
      

      希望这会有所帮助!

      【讨论】:

        【解决方案3】:

        newsFeedFragment 已声明但未在 MainActivity 类中定义。 NullPointerException 主要是在我们声明任何对象但未定义该对象时引起的。

        NewsFeedFragment newsFeedFragment;////// this is declaration
        newsFeedFragment=NewsFeedFragment();////// this is definition of that declared object
        

        现在在定义之后你可以在任何你想要的地方使用这个对象。 喜欢

        setFragment(newsFeedFragment);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多