Note that there are some explanatory texts on larger screens.

plurals
  1. POAsyctask generating future task exception
    primarykey
    data
    text
    <p>I want to create a reminder</p> <p>I have created a database. Database stores date and time. upto here everything is working. Now, I want to run an <code>asynctask</code> in background which checks after every few seconds if there is new reminder if yes then call to another activity using intent that generate <code>notificaton</code>. Also for checking in to database it is calling a different activity. I have created an infinite while loop so that it keeps on checking.</p> <p>here is code.In this I am comparing current date and time with the date date and time stored in database.The complete logic of comparison is written in reminder activity.Create_notification is an Activity data has the logic of creating notification</p> <pre><code>public class Async_task extends AsyncTask&lt;Void, Void, ArrayList&lt;String&gt;&gt;{ MainActivity async_task_context = null; public Async_task(MainActivity context) { async_task_context = context; // TODO Auto-generated constructor stub } @Override protected ArrayList&lt;String&gt; doInBackground(Void... params) { // TODO Auto-generated method stub Log.d("in async task", "msg"); while(1&lt;2){ Calendar cal = Calendar.getInstance(); int current_day = cal.get(Calendar.DAY_OF_MONTH); Log.d("in do in background", ""); int cuurent_month = cal.get(Calendar.MONTH); int current_year = cal.get(Calendar.YEAR); int curent_hour = cal.get(Calendar.HOUR_OF_DAY); int current_minute = cal.get(Calendar.MINUTE); String date = current_day+"/"+cuurent_month+"/"+current_year; String time = curent_hour+":"+current_minute; ArrayList&lt;String&gt; arr = new ArrayList&lt;String&gt;(); Reminder fetch_info = new Reminder(async_task_context); fetch_info.open(); arr = fetch_info.fetch(date,time); fetch_info.close(); Log.d("arr in oncreate", ""+arr); int size = arr.size(); if(size !=0){ Intent in = new Intent(async_task_context,Create_notification.class); in.putExtra("information", arr); try { Thread.sleep(9000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return arr; } </code></pre> <p>On running <code>logcat</code> I came across the following errors</p> <pre><code>08-30 18:13:18.701: E/AndroidRuntime(797): java.lang.RuntimeException: An error occured while executing doInBackground() 08-30 18:13:18.701: E/AndroidRuntime(797): at android.os.AsyncTask$3.done(AsyncTask.java:299) 08-30 18:13:18.701: E/AndroidRuntime(797): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 08-30 18:13:18.701: E/AndroidRuntime(797): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 08-30 18:13:18.701: E/AndroidRuntime(797): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 08-30 18:13:18.701: E/AndroidRuntime(797): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 08-30 18:13:18.701: E/AndroidRuntime(797): at android.os.Handler.&lt;init&gt;(Handler.java:197) 08-30 18:13:18.701: E/AndroidRuntime(797): at com.Itspark.smartreminder.Reminder.&lt;init&gt;(Reminder.java:74) 08-30 18:13:18.701: E/AndroidRuntime(797): at com.Itspark.smartreminder.Async_task.doInBackground(Async_task.java:36) </code></pre> <p>Below is reminder class.Reminder class is creating database and it has many methode.</p> <pre><code>public class Reminder extends Activity{ public static final String Column_Id="_Id"; private static final String name = "Event_Name"; private static final String date = "Event_date"; private static final String time = "Event_time"; private static final String location = "Event_location"; private static final String Table_Name="Data_container"; private static final String Database_Name="Data_Ship"; private static final int Version=1; private Context ourcontext; private SQLiteDatabase ourSQLiteDatabase; private DbHelper ourHelper; private static class DbHelper extends SQLiteOpenHelper{ public DbHelper(Context context) { super(context, Database_Name, null, Version); } @Override public void onCreate(SQLiteDatabase db) { Log.d("in on create", "before"); db.execSQL("CREATE TABLE "+ Table_Name + "(" +Column_Id+" INTEGER PRIMARY KEY AUTOINCREMENT, " + name +" TEXT NOT NULL, " + date +" TEXT NOT NULL, "+ time +" TEXT NOT NULL, "+ location +" TEXT );"); Log.d("in on create", ""); } @Override public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) { db.execSQL("DROP TABLE IF EXISTS "+Table_Name); onCreate(db); } } public Reminder(Context c) { //**This is line 74** ourcontext = c; } public Reminder open(){ ourHelper = new DbHelper(ourcontext); ourSQLiteDatabase = ourHelper.getWritableDatabase(); return this; } public void close(){ ourHelper.close(); } public ArrayList&lt;String&gt; fetch(String date2, String time2) { // TODO Auto-generated method stub String anArray[] = new String[] {Column_Id,name,date,time,location}; Cursor cu = ourSQLiteDatabase.query(Table_Name, anArray, date+" =?"+" AND "+time+" =?", new String[] {date2,time2}, null, null, null); Log.d("after cursor","ere"); int name1=cu.getColumnIndex(name); int date1 = cu.getColumnIndex(date); int time1 = cu.getColumnIndex(time); int location1 = cu.getColumnIndex(location); ArrayList&lt;String&gt; arr = new ArrayList&lt;String&gt;(); for(cu.moveToFirst();!cu.isAfterLast();cu.moveToNext()){ arr.add("Name "+cu.getString(name1)+ "\nDate "+cu.getString(date1)+"\nTime "+cu.getString(time1)+"\nLocation "+cu.getString(location1)); } return arr; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload