Search code examples
androidandroid-listviewandroid-activityandroid-cursorloader

Android application crashes on starting an Activity with message 'FATAL EXCEPTION: Async #3'


I am trying to make an application where I need to generate a ListView with data retrieved from a SQLite database.

But as soon as an activity is initiated which does the above task, the application crashes.

Here's the activity code:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class DisplayTasks extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor>{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            SimpleCursorAdapter adapter;
            String fromColumns[] = {TaskEntryDetails.TASK_SUMMARY};
            int toViews[] = {R.id.textView1};

            adapter = new SimpleCursorAdapter(this, R.layout.list_item, null, fromColumns, toViews, 0);
            setListAdapter(adapter);

            // Prepare the loader.  Either re-connect with an existing one, or start a new one.
            getLoaderManager().initLoader(0, null, this);
        }


    private SQLiteCursorLoader loader;
    private SimpleCursorAdapter adapter;
    private DbHelper dbHelper;
    private SQLiteOpenHelper db=null;

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    //New
    TaskEntry taskentry = new TaskEntry(DisplayTasks.this);
    dbHelper=taskentry.DbHelpermethod();

    db = dbHelper;
    String rawQuery = "SELECT " + TaskEntryDetails._ID + " , " + TaskEntryDetails.TASK_SUMMARY + " FROM " + TaskEntryDetails.TABLE_NAME;
    loader = new SQLiteCursorLoader(this, db, rawQuery, null);
    return(loader);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        adapter.changeCursor(cursor);
}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
        adapter.changeCursor(null);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_display_tasks, menu);
    return true;
}
}

And the logcat report contains:

03-07 19:38:12.311: E/AndroidRuntime(32626): Caused by: java.lang.NullPointerException
03-07 19:38:12.311: E/AndroidRuntime(32626):    at com.commonsware.cwac.loaderex.SQLiteCursorLoader.buildCursor(SQLiteCursorLoader.java:54)
03-07 19:38:12.311: E/AndroidRuntime(32626):    at com.commonsware.cwac.loaderex.AbstractCursorLoader.loadInBackground(AbstractCursorLoader.java:41)
03-07 19:38:12.311: E/AndroidRuntime(32626):    at com.commonsware.cwac.loaderex.AbstractCursorLoader.loadInBackground(AbstractCursorLoader.java:1)
03-07 19:38:12.311: E/AndroidRuntime(32626):    at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:301)
03-07 19:38:12.311: E/AndroidRuntime(32626):    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:68)
03-07 19:38:12.311: E/AndroidRuntime(32626):    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:56)
03-07 19:38:12.311: E/AndroidRuntime(32626):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-07 19:38:12.311: E/AndroidRuntime(32626):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-07 19:38:12.311: E/AndroidRuntime(32626):    ... 3 more
03-07 19:38:12.321: W/DropBoxManagerService(481): Dropping: data_app_crash (1407 > 0 bytes)
03-07 19:38:12.331: W/ActivityManager(481):   Force finishing activity com.example.theapp/.DisplayTasks
03-07 19:38:12.361: W/ActivityManager(481):   Force finishing activity com.example.theapp/.MainActivity
03-07 19:38:12.481: I/ActivityManager(481): Displayed com.example.theapp/.DisplayTasks: +219ms
03-07 19:38:14.381: I/Process(32626): Sending signal. PID: 32626 SIG: 9
03-07 19:38:14.391: I/ActivityManager(481): Process com.example.theapp (pid 32626) has died.
03-07 19:38:14.411: W/InputMethodManagerService(481): Got RemoteException sending setActive(false) notification to pid 32626 uid 10083

Here's the code for TaskEntry.java

public static final String TAG = "TaskEntry";
private DbHelper dbHelper;

public TaskEntry(Context context){
    dbHelper = new DbHelper(context);
}
public DbHelper DbHelpermethod() {
    return this.dbHelper();
}

// Inserting into Database
public void insertIntoDb(String summary) {
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    // Create a new map of values, where column names are the keys
    ContentValues values = new ContentValues();
    values.put(TaskEntryDetails.TASK_SUMMARY, summary);

    @SuppressWarnings("unused")
    long newRowId;
    newRowId = db.insert(TaskEntryDetails.TABLE_NAME, TaskEntryDetails._ID, values);
}

and DbHelper.java:

public class DbHelper extends SQLiteOpenHelper {

public static final String TAG = "TaskEntry";
private static Context context;

public DbHelper(Context context) {
    super(context, TaskEntryDetails.DB_NAME, null, TaskEntryDetails.DB_VERSION);
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = String.format("CREATE TABLE %s" + "(%s INT PRIMARY KEY, %s TEXT)", TaskEntryDetails.TABLE_NAME, TaskEntryDetails._ID,TaskEntryDetails.TASK_SUMMARY);

    Log.d(TAG, "Created database by name - " + TaskEntryDetails.DB_NAME);
    db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP IF EXISTS " + TaskEntryDetails.TABLE_NAME);
    onCreate(db);
}

TaskEntryDetails.java:

public class TaskEntryDetails implements BaseColumns {
public static final String DB_NAME = "tasks_list.db";
public static final int DB_VERSION = 1;

public static final String TABLE_NAME = "tasks_list";
public static final String TASK_SUMMARY = "task_details";
}

I am currently using an open-source library 'CWAC-LoaderEX' to get a CursorLoader for the data in SQLite database.

I have successfully retrieved and displayed data from the same database into a ScrollView so hopefully the issue must not be with the database.

EDIT 1:

The code seems to crash with another message now..

03-08 20:37:10.031: D/libEGL(9969): loaded /system/lib/egl/libEGL_tegra.so
03-08 20:37:10.071: D/libEGL(9969): loaded /system/lib/egl/libGLESv1_CM_tegra.so
03-08 20:37:10.091: D/libEGL(9969): loaded /system/lib/egl/libGLESv2_tegra.so
03-08 20:37:10.141: D/OpenGLRenderer(9969): Enabling debug mode 0
03-08 20:37:12.411: D/AndroidRuntime(9969): Shutting down VM
03-08 20:37:12.411: W/dalvikvm(9969): threadid=1: thread exiting with uncaught exception (group=0x40c28930)
03-08 20:37:12.421: E/AndroidRuntime(9969): FATAL EXCEPTION: main
03-08 20:37:12.421: E/AndroidRuntime(9969): java.lang.NullPointerException
03-08 20:37:12.421: E/AndroidRuntime(9969):     at com.example.theapp.DisplayTasks.onLoadFinished(DisplayTasks.java:83)
03-08 20:37:12.421: E/AndroidRuntime(9969):     at com.example.theapp.DisplayTasks.onLoadFinished(DisplayTasks.java:1)
03-08 20:37:12.421: E/AndroidRuntime(9969):     at android.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:483)
03-08 20:37:12.421: E/AndroidRuntime(9969):     at android.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:451)
03-08 20:37:12.421: E/AndroidRuntime(9969):     at android.content.Loader.deliverResult(Loader.java:143)
03-08 20:37:12.421: E/AndroidRuntime(9969):     at com.commonsware.cwac.loaderex.AbstractCursorLoader.deliverResult(AbstractCursorLoader.java:71)
03-08 20:37:12.421: E/AndroidRuntime(9969):     at com.commonsware.cwac.loaderex.AbstractCursorLoader.deliverResult(AbstractCursorLoader.java:1)

Solution

  • The problem was initially with correctly initializing the database. It should have been done as:

    The class TaskEntry ( in TaskEntry.java )

    public class TaskEntry {
    
    public static final String TAG = "TaskEntry";
    private DbHelper dbHelper;
    
    public TaskEntry(Context context){
        dbHelper = new DbHelper(context);
    }
    public DbHelper DbHelpermethod() {
        return this.dbHelper;
    }
    ...}
    

    Also, the LoaderManager was not implemented correctly..

    After the onCreateLoader (which was rather fine).. a cursor is initialized, which doesn't get passed into the onLoadFinished and onLoaderReset calls. They receive the old adapter here which is initialized with 'null' as cursor value. Ref:

    adapter = new SimpleCursorAdapter(this, R.layout.list_item, null, fromColumns, toViews, 0);
    

    This is fixed by replacing:

    adapter.changeCursor(cursor);
    

    in onLoadFinished and

    adapter.changeCursor(null);
    

    in onLoaderReset in the DisplayTasks.java activity by:

    ((SimpleCursorAdapter)this.getListAdapter()).changeCursor(cursor);
    

    and..

    ((SimpleCursorAdapter)this.getListAdapter()).changeCursor(null);
    

    repectively..