-1

I simply want to call a method declared in fragment through parent activity and it gives me the error " Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference"` Please suggest something.

Fragment Method:

public void loadSpinnerData() {
    Toast.makeText(mActivity, "HELLO", Toast.LENGTH_SHORT).show();
}

Calling method from parent actvity:

private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new NoticeBoard();
        break;
    case 1:
        fragment = new Quotes();
        if (fragment instanceof Quotes)
            ((Quotes) fragment).loadSpinnerData();
        break;
    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer

    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
} 

my logcat is as follows

Logcat error : java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 05-25 08:46:59.740: E/AndroidRuntime(6152): at app.redalkemi.edublaze.Quotes.loadSpinnerData(Quotes.java:210)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Aparana
  • 59
  • 1
  • 11

1 Answers1

0

try changing your code a little bit

    public void loadSpinnerData(Context contxt) {
    Toast.makeText(contxt, "HELLO", Toast.LENGTH_SHORT).show();
}

in parent activity

 if (fragment instanceof Quotes)
        ((Quotes) fragment).loadSpinnerData(youractivityname.this);
Karthika PB
  • 1,373
  • 9
  • 16
  • no it's giving the same error – Aparana May 25 '15 at 07:58
  • post your logcat and mention in which line are you getting the error – Karthika PB May 25 '15 at 08:53
  • i have posted the logcat please review – Aparana May 25 '15 at 09:28
  • post your updated code with mine and in which line do you get the error??? tell me – Karthika PB May 25 '15 at 10:15
  • Logcat error : java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 05-25 08:46:59.740: E/AndroidRuntime(6152): at app.redalkemi.edublaze.Quotes.loadSpinnerData(Quotes.java:210) – Aparana May 25 '15 at 10:49
  • what did u pass in ((Quotes) fragment).loadSpinnerData(youractivityname.this); as parameter post the line you tried.. – Karthika PB May 25 '15 at 10:59
  • i didn't pass anything with ((Quotes) fragment).loadSpinnerData(youractivityname.this); as paramamerter – Aparana May 25 '15 at 11:27
  • have you used my code???You need to pass your context in ((Quotes) fragment).loadSpinnerData(context) which is equal to ((Quotes) fragment).loadSpinnerData(youractivityname.this); replace youractivityname with the original activity name of yours – Karthika PB May 25 '15 at 11:31
  • yes i used your code but it didn't work, can you tell me how to pass context in ((Quotes) fragment).loadSpinnerData(context). I'm totally new to fragment – Aparana May 25 '15 at 12:02
  • you are calling this from your activity right? – Karthika PB May 25 '15 at 12:30
  • yes I'm calling this: ((Quotes) fragment).loadSpinnerData(youractivityname.this); from MainActivity – Aparana May 25 '15 at 12:50
  • ohhh... it's not youractivity.this replace it with your classname.ie:the name of your parent activity. – Karthika PB May 26 '15 at 03:56
  • solve the problem. Thanks...... your really very helpful – Aparana May 26 '15 at 06:27