继续在findViewById上获取空引用

时间:2018-06-01 03:35:02

标签: android imagebutton android-navigation-drawer

我正在尝试在Android Studio中使用ImageButton打开和关闭导航抽屉。我的困难在于我无法使用某些方法,因为我在一个支离破碎的子类中。这是我得到的错误。

  

引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.view.View android.view.View.findViewById(int)'           在dadump.test.HomeFragment.onCreateView(HomeFragment.java:27)

以下是导致错误的代码。我的家庭活动中还有一个名为open()的方法,只有一行:mDrawerLayout.openDrawer(Gravity.LEFT);

public class HomeFragment extends Fragment {

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;

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

    ImageButton button = (ImageButton)getView().findViewById(R.id.button_settings);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(android.view.View view) {
            ((MainActivity)getActivity()).open();
        }
    });
    return View;
    }
}

主类:

    public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private DrawerLayout mDrawerLayout;

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

        BottomNavigationView navigation =findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(this);

        setupBottomNavigationView();
        loadFragment(new HomeFragment());


    }

    private void setupBottomNavigationView(){
        BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx)findViewById(R.id.navigation);
        BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
    }

    private boolean loadFragment(android.support.v4.app.Fragment fragment) {

        if(fragment != null){

            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();

            return true;
        }
        return false;
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        android.support.v4.app.Fragment fragment = null;

        switch(item.getItemId()){
            case R.id.ic_home:
                fragment = new HomeFragment();
                break;
            case R.id.ic_directMessage:
                fragment = new DirectMessageFragment();
                break;
            case R.id.ic_newPost:
                fragment = new NewPostFragment();
                break;
            case R.id.ic_notifications:
                fragment = new NotificationsFragment();
                break;
            case R.id.ic_userProfile:
                fragment = new UserProfileFragment();
                break;
        }

        return loadFragment(fragment);
    }

    public void open() {
        mDrawerLayout.openDrawer(Gravity.LEFT);
    }
}

1 个答案:

答案 0 :(得分:0)

onCreateView getView()尚未初始化。这就是创建此问题的原因。尝试使用view代替getView()。如果您想从getView()获取数据,请将此代码移至onViewCreated() @Override方法。

 View view = inflater.inflate(R.layout.fragment_home, null);

ImageButton button = (ImageButton)view.findViewById(R.id.button_settings);

这将解决您的问题。