简体   繁体   中英

null Object :Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference

I am making a university app but does not getting what is the problem with my app, because it is not running. Can anyone help me to figure it out? This is the code. I am doing this in a navigation Bar Activity.

public class Sbbu_App extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
private ActivitySbbuAppBinding binding;

    ArrayList<Integer> mImageIds = new ArrayList<>(Arrays.asList(
            R.drawable.logo,R.drawable.gradient,R.drawable.dr, R.drawable.logo,R.drawable.gradient,
            R.drawable.logo,R.drawable.gradient,R.drawable.dr, R.drawable.logo,R.drawable.gradient,
            R.drawable.logo,R.drawable.gradient,R.drawable.dr, R.drawable.logo,R.drawable.gradient

    ));
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GridView gridView = findViewById(R.id.myGrid);
        gridView.setAdapter(new ImageAdapter(new ImageAdapter(mImageIds,this)));

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int item_pos =mImageIds.get(position);
            }
        });

     binding = ActivitySbbuAppBinding.inflate(getLayoutInflater());
     setContentView(binding.getRoot());

        setSupportActionBar(binding.appBarSbbuApp.toolbar);

        DrawerLayout drawer = binding.drawerLayout;
        NavigationView navigationView = binding.navView;
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.ABOUT_SBBU, R.id.nav_gallery, R.id.Academics, R.id.login, R.id.newsEvents)
                .setOpenableLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_sbbu_app);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }
    public void ShowDialougeBox(int item_pos){
        Dialog dialog = new Dialog(this);

        dialog.setContentView(R.layout.custome_dialog);

        TextView Image_name = dialog.findViewById(R.id.txt_Image_name);
        ImageView Image = dialog.findViewById(R.id.img);
        Button btn_Full = dialog.findViewById(R.id.btn_full);
        Button btn_Close = dialog.findViewById(R.id.btn_close);

        String title =getResources().getResourceName(item_pos);

        int index = title.indexOf("/");
        String name =title.substring(index+1,title.length());
        Image_name.setText(name);

        btn_Close.setOnClickListener((new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        }));
        btn_Full.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent i =new Intent(Sbbu_App.this, FullView.class);
            i.putExtra("img_id",item_pos);
            startActivity(i);
            }
        });
        dialog.show();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sbbu__app, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_sbbu_app);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}

You cannot call findViewById before you have called setContentView or it will always return null. You got a NullPointerException because you called setAdapter on gridView but gridView was null.

You should move your call to setContentView up before anything involving views, and since you are using view binding, you don't need to call findViewById at all - just get the view from the binding class.

The binding class automatically calls findViewById when you inflate or bind it then saves all the views from your layout as class members so you don't have to do that manually.

Like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = ActivitySbbuAppBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    binding.myGrid.setAdapter(new ImageAdapter(new ImageAdapter(mImageIds,this)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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