Search code examples
androidnullpointerexceptiononbackpressed

NullpointerExpection returned when back pressed


I am sending the value from Activity A to Activity B and data is set to the view, it is working fine but when I press back button it is returning the nullpointerexpection in textView.setText() method . I have implemented as follows:

 Intent i = getIntent();
    serialArrayList = i.getParcelableExtra("member_list");

    gridView = (GridView) findViewById(R.id.officerGrid);
    adapter = new DistrictOfficerGridAdapter(this, serialArrayList);
    gridView.setAdapter(adapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
             startActivity(new Intent(getApplicationContext(), MemberDetailsActivity.class));
            Intent in = new Intent(DistrictOfficerActivity.this, MemberDetailsActivity.class);
            in.putExtra("name", serialArrayList.getTitle());
            in.putExtra("desig", serialArrayList.getPosition());
            in.putExtra("contact", serialArrayList.getMobile());
            in.putExtra("addr", serialArrayList.getAddress());
            in.putExtra("blood", serialArrayList.getBloodGroup());
            in.putExtra("image",serialArrayList.getPhotos());
            in.putExtra("email", serialArrayList.getEmail());
            in.putExtra("desc", serialArrayList.getDesc());
            in.putExtra("website", serialArrayList.getWebsite());
            in.putExtra("land", serialArrayList.getLandline());
            startActivity(in);

        }
    });

And the data is received from MemberDetailsActivity and implemented as follows:

    nam = getIntent().getStringExtra("name").toString();
        design = getIntent().getStringExtra("desig");
        contat = getIntent().getStringExtra("contact");
        addr = getIntent().getStringExtra("addr");
        blood = getIntent().getStringExtra("blood");
        image = getIntent().getStringExtra("image");
        website = getIntent().getStringExtra("website");
        email = getIntent().getStringExtra("email");
        landL = getIntent().getStringExtra("land");

   name.setText(nam.toString());
    desig.setText(design.toString());
    phoneNumber.setText(contat.toString());
    address.setText(addr.toString());
    bloodG.setText(blood.toString());
    Glide.with(getApplicationContext()).load(image).into(imag);
    web.setText(website.toString());
    emai.setText(email.toString());
    landline.setText(landL.toString());

Solution

  • You do one mistake here. You are calling your Activity twice so please remove one of them. Try with this

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent in = new Intent(DistrictOfficerActivity.this, MemberDetailsActivity.class);
            in.putExtra("name", serialArrayList.getTitle());
            in.putExtra("desig", serialArrayList.getPosition());
            in.putExtra("contact", serialArrayList.getMobile());
            in.putExtra("addr", serialArrayList.getAddress());
            in.putExtra("blood", serialArrayList.getBloodGroup());
            in.putExtra("image",serialArrayList.getPhotos());
            in.putExtra("email", serialArrayList.getEmail());
            in.putExtra("desc", serialArrayList.getDesc());
            in.putExtra("website", serialArrayList.getWebsite());
            in.putExtra("land", serialArrayList.getLandline());
            startActivity(in);
    
        }
    });