0
@Override
protected Object doInBackground(Object... params) 
{
    strFullJsonResponse = new LoginService(c).tabsDataService(tabId);
    try {
        JSONObject jsonObject = new JSONObject(strFullJsonResponse);
        String json = jsonObject.getString("output");
        JSONObject object = new JSONObject(json);
        banner = object.getString("bannerurl");
        ELUtil.setPreference(c, "banner", banner);


        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("bannerurl", banner);
        String bookInfoResponse = object.getString("groupinfo");
        Log.i("EL", "group info is"+bookInfoResponse);
        JSONArray jsonArray = new JSONArray(bookInfoResponse);
        jsonArrayObject.getString("description"));

    } 
    catch (Exception ex) 
    {
        ex.printStackTrace();
    }
    return null;
}

}

and this is my setPreference()

public static void setPreference(Context context, String key, String value) {
    SharedPreferences preferences = ELUtil.getSharedPrefrenceInstance(context);
    Editor editor = preferences.edit();
    editor.putString(key, value).commit();
}

and this is my getSharedPreferenceInstance()

public static SharedPreferences getSharedPrefrenceInstance(Context mContext) 
{
    SharedPreferences preferences = mContext.getSharedPreferences("le", 0);
    return preferences;
}

ELutil is my own class where I am having my own setPreference method and here is my logcat

09-08 06:32:10.040: W/System.err(17628): java.lang.NullPointerException
09-08 06:32:10.210: I/Choreographer(17628): Skipped 289 frames!  The application may be doing too much work on its main thread.
09-08 06:32:10.260: D/gralloc_goldfish(17628): Emulator without GPU emulation detected.
09-08 06:32:10.290: W/System.err(17628):    at com.tab.tabswipe.ELUtil.getSharedPrefrenceInstance(ELUtil.java:109)
09-08 06:32:10.290: W/System.err(17628):    at com.tab.tabswipe.ELUtil.setPreference(ELUtil.java:159)
09-08 06:32:10.300: W/System.err(17628):    at com.tab.async.TabContentAsyncTask.doInBackground(TabContentAsyncTask.java:40)
09-08 06:32:10.300: W/System.err(17628):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-08 06:32:10.300: W/System.err(17628):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-08 06:32:10.300: W/System.err(17628):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-08 06:32:10.300: W/System.err(17628):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-08 06:32:10.300: W/System.err(17628):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-08 06:32:10.300: W/System.err(17628):    at java.lang.Thread.run(Thread.java:841)
Maveňツ
  • 1
  • 12
  • 50
  • 89
user095736
  • 77
  • 3
  • 13

2 Answers2

0

Try like this

@Override
protected Object doInBackground(Object... params) 
{
strFullJsonResponse = new LoginService(c).tabsDataService(tabId);
try {
    JSONObject jsonObject = new JSONObject(strFullJsonResponse);
    String json = jsonObject.getString("output");
    JSONObject object = new JSONObject(json);
    banner = object.getString("bannerurl");
    c=getBaseContext();
    ELUtil.setPreference(c, "banner", banner);


    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("bannerurl", banner);
    String bookInfoResponse = object.getString("groupinfo");
    Log.i("EL", "group info is"+bookInfoResponse);
    JSONArray jsonArray = new JSONArray(bookInfoResponse);
    jsonArrayObject.getString("description"));

} 
catch (Exception ex) 
{
    ex.printStackTrace();
}
return null;
}

}

update

  1. getApplicationContext () returns the application context of the entire application life cycle,when application will destroy then it will destroy also.

  2. this the context returns the current context of the activity, belong to the activity, the activity is destroyed then it will destroy also.

  3. getBaseContext() is the method of ContextWrapper. And ContextWrapper is, "Proxying implementation of Context that simply delegates all of its calls to another Context. Can be subclassed to modify behavior without changing the original Context." (as per javadocs)

Maveňツ
  • 1
  • 12
  • 50
  • 89
0

Check my answer about how to pass values from an AsyncTask to an Activity: https://stackoverflow.com/a/25262628/3465623

Community
  • 1
  • 1
luiscosta
  • 855
  • 1
  • 10
  • 16