0

i have two fragments, i want use callback using interface to change imageview in first fragment from the second fragment but when callback is fired its get imageview null and i get the below error

     java.lang.NullPointerException: Attempt to invoke virtual method 'void 
      android.widget.ImageView.setImageResource(int)' on a null object 
    reference

first fragment:

   public class firstfragment extends Fragment implements MyProfileCallback
   {
  Imageview myprofile_image;

   public firstfragment()
  {
// Required empty public constructor
  }

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

    @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState)
       {
       View rootview = inflater.inflate(R.layout.fisrt, container, false);

  myprofile_image=(ImageView) rootview.(find.id.myprofile_image);
      /
     ...
    /       
      }
     @Override
  public void callbackCall()
      {

      myprofile_image.setImageResource(R.drawable.profile_friends);

    }
    }

second fragment:

     public class secondfragment extends Fragment
        {

       MyProfileCallback mcallback;

   public secondfragment()
     {
       // Required empty public constructor
    }


     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,     

Bundle savedInstanceState)
     {
      View rootview = inflater.inflate(R.layout.second, container, false);


    mcallback.callbackCall();


   return rootview;
      }

interface

    public interface MyProfileCallback

    {
     void callbackCall();

    }
Mina Farid
  • 5,041
  • 4
  • 39
  • 46
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Sep 07 '16 at 16:10

1 Answers1

1

Your question is kind of hard to understand so is your code, BUT, I think you should load the resource file in this case R.drawable.profile_friends when the fragment view has being created

You can:

EDIT: You are calling your callback from the onCreateView method meaning your mypfile_image view does not have a reference to your element yet and you get a null pointer exception

  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
          mcallback.callbackCall();
        }
Joel
  • 838
  • 5
  • 12
  • i used this code but i have the same error, i want to change image from another activity using callback – Mina Farid Apr 22 '16 at 19:14
  • Does the another activity is being opened? Then if so you need to get a reference of your ImageView from it's onCreate/onCreateView depending what you are using (Activity of Fragment) and set it from there, if you are doing it backwards it won't work because myprofile_image does not belong to the Activity layout that you are using to make the call. – Joel Apr 22 '16 at 19:17
  • iam using fragments , thus how to get refernce of myprofile_image in the second fragment to pass it to the first fragment which contains the image view ? @Magnar – Mina Farid Apr 22 '16 at 19:23
  • Are both fragments in the same Activity? – Joel Apr 22 '16 at 19:27
  • @MinaFared did you checked my fixed answer and solved it ? – Joel Apr 27 '16 at 02:53