2

In class socialNetworksActivity i have a method:

public void getResultFromFBPost(String result)
{
  if (result.equals("SUC"))
    ShowToast("Your message have been posted successfully", Toast.LENGTH_LONG);
  else
    ShowToast("There was an error posting,"
        + " please check your Internt connection", Toast.LENGTH_LONG);
}

Which calls this method (also in class socialNetworksActivity):

public void ShowToast(String message, int duration)
{
    LayoutInflater inflater = getLayoutInflater();
          ....
}

When I call this method from inside the class it works fine.

But when I call this method from class B like this:

@Override
protected void onPostExecute(String result)
{
  socialNetworksActivity.getResultFromFBPost(result);
  pd.dismiss();
}

I get a NullPointerException when code get to:

LayoutInflater inflater = getLayoutInflater();

Any idea why?

Aman Agnihotri
  • 2,973
  • 1
  • 18
  • 22
Michael A
  • 5,770
  • 16
  • 75
  • 127
  • Where did you LayoutInflater inflater = getLayoutInflater(); called this in Activity or any other class? – M D Mar 01 '14 at 18:35
  • On my opPostExcecute "getResultFromDBPost" is called which invokes this call in "showToast" method – Michael A Mar 01 '14 at 18:38
  • issue fixed if not then can u paste getLayoutInflater method – Kick Mar 01 '14 at 18:38
  • kindly go to this:[http://stackoverflow.com/questions/7803771/call-to-getlayoutinflater-in-places-not-in-activity](http://stackoverflow.com/questions/7803771/call-to-getlayoutinflater-in-places-not-in-activity) – M D Mar 01 '14 at 19:00
  • Please post complete complete stacktrace. – laalto Mar 01 '14 at 19:17
  • @SimplePlan Thanks, but my case here is diffrent - my Show Toast method is not static, and i also inflate a custom layout after i get the inflater: "View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) ((Activity) context).findViewById(R.id.custom_toast_layout));" – Michael A Mar 01 '14 at 19:38

2 Answers2

1

I'm not sure why the LayoutInflater is coming back null but another way to get the LayoutInflater is to simply call LayoutInflater.from(this) or, if this isn't a Context, some other Context, maybe by using the method getContext() or getApplicationContext() depending on the class you're in.

SDJMcHattie
  • 1,690
  • 1
  • 15
  • 21
1

I think it's a problem with calling it from async task. Call it from the Activity or if you don't want to do that pass activity context into AsyncTask and call it on that object. Also be sure to call it on the UI thread (althou you are already doing that by using onPostExecute but just keep that in mind).

Igor Čordaš
  • 5,785
  • 4
  • 42
  • 54