mercredi 10 juin 2015

Android Database in AsyncTask in Fragment

I have a DbHelper Class which extends SQLiteOpenHelper. I do Some Download and update the Database inside an Asynctask. Inside an activity i got no problem and code works fine, but when i use the ASynctask class inside a fragment problems occurs.

usually wherever i use a context an Exception happened, Especially with dbHelper.ClearDB()

Error:

DB Read ERROR:java.lang.NullPointerException:
 Attempt to invoke     virtual method 'java.util.ArrayList x.database.DBHelper.getAllItems()' on a null object reference

Here's the code :

public class StaggeredFragment extends Fragment
{


private DBHelper dbHelper;
private SharedPreferences preferences;
private ArrayList<DisItem> savedData;
private final String LINK1 = "myLink";

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

    dbHelper = new DBHelper(getActivity().getApplicationContext());
    preferences = getActivity().getSharedPreferences("pid", Context.MODE_PRIVATE);

    new LoaderAsyncTask("ALL").execute();
}

class LoaderAsyncTask extends AsyncTask<Void, Void, Boolean> {

    String brand;

    LoaderAsyncTask(String brand) {
        this.brand = brand;
    }


    @Override
    protected Boolean doInBackground(Void... params) {

        Log.d(TAG,"RUnning");

        String fetched;
        InputStream is = null;

        //Store Current Data before Sync
        try {
            savedData = dbHelper.getAllItems();
        }catch (Exception e)
        {
            Log.d(TAG,"DB Read ERROR:"+e.toString());
            return false;
        }

        try {
            dbHelper.ClearDB();
        }catch (Exception e)
        {
            Log.d(TAG,"DB Clear ERROR:"+e.toString());
            return false;
        }

//        Open connection to server for html
        try {
            is = urlStream(LINK1);
        } catch (Exception e) {
            Log.e(TAG, "HTTP Error " + e.toString());
            return false;
        }

 //        Fetch HTML Data
        try {
            fetched = readIt(is);
           // Log.d("fetched", fetched);
        } catch (Exception e) {
            Log.e(TAG, "Buffer Error " + e.toString());
            return false;
        }

  //        Parsing JSON
        try {
            if (!fetched.isEmpty())
                InitialsJson(fetched);
        }catch (JSONException e) {
            Log.e(TAG, "JSON Error " + e.toString());
            return false;
        }


        return true;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        if(!aBoolean)
            RestoreData();
    }

}

private void InitialsJson(String fetched) throws JSONException
{
    JSONObject jsonObject = new JSONObject(fetched);

    if (jsonObject.getInt("success") == 1) {
        JSONArray array = jsonObject.getJSONArray("data");
        for (int i = 0; i<array.length() ; i++) {

            JSONObject object = array.getJSONObject(i);
            DisItem disItem = new DisItem();
               disItem.setPid(object.getString("pid"));
 disItem.setLiked(preferences.getBoolean(String.valueOf(disItem.getPid()), false));

            Log.d(TAG, disItem.toString());
            dbHelper.insert(disItem);

        }


    }
}

This is Databace getallItems function

      public ArrayList<DisItem> getAllItems()
      {
       SQLiteDatabase db = this.getReadableDatabase();
       Cursor cursor = db.rawQuery("select * from " + DIS_TABLE_NAME + "", null);
        ArrayList<DisItem> arrayList = new ArrayList<>();
       cursor.moveToFirst();

    while (! cursor.isAfterLast())
    {
        DisItem disItem = new DisItem(cursor);
        arrayList.add(disItem);
        cursor.moveToNext();
    }
    return arrayList;

}

Aucun commentaire:

Enregistrer un commentaire