jeudi 17 décembre 2015

SQlite error from asynctask

I am trying to build event lister. I am taking events from some sites, then record to database and then show.

That is my updatedb class called when button activated from main:

public class UpdateDB {
    private String TAG = "Chic";
    EventRetriever eventRetriever;
    private Activity mActivity;

     public UpdateDB(Activity activity){
        Log.e(TAG,"UpdateDB constructor");
        this.mActivity = activity;

        eventRetriever = new EventRetriever(mActivity);
        eventRetriever.execute();
        Log.e(TAG,"UpdateDB constructor async executed");
        currentDate = Calendar.getInstance(); //Get the current date
        lastUpdateTime=currentDate;//if succesful methodu olması lazım
        Log.e(TAG,"DB updated");
    }

This is inside of update db:

class EventRetriever extends AsyncTask<Void,Void,List<Event>> {

 Event evnt;
    private List<Event> evnts;
    DBHandler dbhandler ;

  public EventRetriever(Activity activity){
        super();
        Log.e(TAG,"EventRetriever constructor");
        mActivity = activity;
    }

   protected void onPreExecute() {
        super.onPreExecute();
        Log.e(TAG,"onPreExecute()");
        dialog = ProgressDialog.show(mActivity,"LUTFEN BEKLEYINIZ...","ETKINLIKLER VERITABANINA KAYIT ICIN HAZIRLANIYOR...", true);
    }

 protected List<Event>  doInBackground(Void ... params) {

        Log.e(TAG,"doinbackground");

//taking each event and then putting into events arraylist

  evnts.add(evnt);

  Log.e(TAG,"doinbackground sonu");
        return evnts;
    }//end of doinbackground

 @Override
    protected void onPostExecute(List<Event> events) {
        super.onPostExecute(events);
        Log.e(TAG,"onPostExecute()");
        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        Log.e(TAG,"ITU db ekleme");

        dbhandler = new DBHandler(mContext);

        for(Event e:events) {
            dbhandler.addEvent(e);
        }


    Log.e(TAG,"endof onPostExecute()");
    }//endof onpostexecute
}//end of asyntask

this is cnstructor of event class and getters setters

 public Event(String nme,String loc,String  typ,String  lnk,String sDate,String eDate,String desc) {
    this.description = desc;
    this.link = lnk;
    this.type = typ;
    this.location = loc;
    this.startDate = sDate;
    this.endDate = eDate;
    this.name = nme;
}

This is my database helper class. i do all maniplutating here

 public class DBHandler  extends SQLiteOpenHelper {

    private String TAG = "Chic";

    SQLiteDatabase db;

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 2;

    // Database Name
    private static final String DATABASE_NAME = "eventsManager";

    // events table name
    private static final String TABLE_EVENTS = "events";

    // EVents Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_LOCATION = "location";
    private static final String KEY_TYPE = "type";
    private static final String KEY_START_TIME = "start_time";
    private static final String KEY_END_TIME = "end time";
    private static final String KEY_DESCRIPTION = "description";
    private static final String KEY_LINK = "link";


    public DBHandler(Context context) {//, Activity mActivity
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.e(TAG,"db handler construct");

  }

public void onCreate(SQLiteDatabase db) {
        Log.e(TAG,"db handleroncreta");
        String CREATE_EVENTS_TABLE = "CREATE TABLE If not exists " + TABLE_EVENTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY autoincrement," + KEY_NAME + " TEXT not null,"
               + KEY_LOCATION + " TEXT not null," + KEY_TYPE + " TEXT not null,"
                + KEY_LINK + " TEXT not null,"
                + KEY_START_TIME + " TEXT not null,"
                + KEY_END_TIME + " TEXT not null,"//bu belki null olabilir
         + KEY_DESCRIPTION + " TEXT not null"//bu da null olabilir
        + ")";

        db.execSQL(CREATE_EVENTS_TABLE);
    }

public void addEvent(Event event) {
        Log.e(TAG,"db handler addevent");
        SQLiteDatabase db = this.getWritableDatabase();

        //db.execSQL("DROP TABLE IF EXISTS "+TABLE_EVENTS);
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, event.getName()); // event Name
        values.put(KEY_START_TIME, event.getStartDate()); // Event start

        // Inserting Rows
        db.insert(TABLE_EVENTS, null, values);
        db.close(); // Closing database connection
    }

public void open() throws SQLException {
        Log.e(TAG,"db handler open");
        db = this.getWritableDatabase();
    }

}//endof class

I did not use cursor. I use values.

Also i had this methodinside event:

public void addEvents(List<Event> events) {
        Log.e(TAG,"db handler addevents: "+events.get(0).getDescription());
      open();

        //db.execSQL("DROP TABLE IF EXISTS "+TABLE_EVENTS);
        for(Event e:events) {
            Log.e(TAG,"ekleniyor: "+e.getName());
            ContentValues values = new ContentValues();
            values.put(KEY_NAME, e.getName()); // event Name
            values.put(KEY_LOCATION, e.getLocation());
            values.put(KEY_TYPE, e.getType());
            values.put(KEY_LINK, e.getLink());
            values.put(KEY_START_TIME, e.getStartDate()); // Event start
            values.put(KEY_END_TIME, e.getEndDate());
            values.put(KEY_DESCRIPTION, e.getDescription());

            // Inserting Rows
            db.insert(TABLE_EVENTS, null, values);
        }//for sonu
        db.close(); // Closing database connection
        Log.e(TAG,"db handler addevents sonu");
    }*/

I was using this to send events as array. Now i amsending one by one but still errors are same. Also another question,which one is good?

Also i tried to send this events inside doitbackground back again errors.

With those codes i posted, errors are:

    AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: cu PID: 28765
                                                                         java.lang.NullPointerException

Kat android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
                                                                             at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
                                                                             at  . .DBHandler.addEvent(DBHandler.java:99)
                                                                             at  . .EventRetriever.onPostExecute(UpdateDB.java:401)
                                                                             at  . .EventRetriever.onPostExecute(UpdateDB.java:73)
                                                                             at android.os.AsyncTask.finish(AsyncTask.java:632)
                                                                             at android.os.AsyncTask.access$600(AsyncTask.java:177)
                                                                             at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:136)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5584)
                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:515)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                                                                             at dalvik.system.NativeStart.main(Native Method)

From main, when i do

Event evt =new Event("name","name","name","name","name","name","name");
        Event evtt =new Event("name","name","name","name","name","name","name");
        List<Event> e = new ArrayList();
        e.add(evt);  e.add(evtt);
      DBHandler d = new DBHandler(this);
        d.addEvents(e);

it writes to database without error. So it cant be null also. I am checkingvalues.

Aucun commentaire:

Enregistrer un commentaire