简体   繁体   English

由 fragmentTransaction 引起的 Android Studio NullPointerException

[英]Android Studio NullPointerException caused by fragmentTransaction

My app stopped working after adding fragments.添加片段后,我的应用程序停止工作。 Particulary this function in MainActivity.java :特别是MainActivity.java 中的这个函数:

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

which is then called inside onCreate :然后在onCreate内部调用:

setFragment(newsFeedFragment);

newsFeedFragment is defined here in MainActivity class: newsFeedFragmentMainActivity类中定义如下:

NewsFeedFragment newsFeedFragment;

And here's the definition of 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;
    }
}

The app crashes and I get this error:该应用程序崩溃,我收到此错误:
在此处输入图片说明

Although it's clearly that function is causing this error, I don't know which part of it is exactly causing the error.虽然很明显是函数导致了这个错误,但我不知道它的哪一部分正是导致错误的原因。
Just in case, here's the whole MainActivity.java ,以防万一,这里是整个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();
    }
}

Because newsFeedFragment is null , initialize your fragment before using it.因为newsFeedFragmentnull ,所以在使用之前初始化你的片段。

newsFeedFragment=NewsFeedFragment();
setFragment(newsFeedFragment);

Hope this will help!!希望这会有所帮助!!

  newsFeedFragment = new NewsFeedFragment();
  setFragment(newsFeedFragment);

newsFeedFragment is declared but not defined in your MainActivity class. newsFeedFragment 已在 MainActivity 类中声明但未定义。 NullPointerException mainly caused if we declare any object but not define that object. NullPointerException 主要是在我们声明任何对象但未定义该对象时引起的。

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

now after definition you can use this object whereever you want.现在在定义之后你可以在任何你想要的地方使用这个对象。 like喜欢

setFragment(newsFeedFragment);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM