Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6948653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:51:04+00:00 2026-05-27T13:51:04+00:00

I’m working with a listview and a context menu. When the user long presses

  • 0

I’m working with a listview and a context menu. When the user long presses an item in the context menu, a little mark appears next to the item in the list, to let the user know the item has been marked a favorite. This is all working well, EXCEPT that it causes a force close the very first time this is done. Every other time works just fine.
Here is the relevant code:

    @Override  
public boolean onContextItemSelected(MenuItem item) {  
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
      case 0:
          mDbHelper.updateFavorite(info.id, 1);
          new bgRequery().execute();
          return true;
      case 1:
          mDbHelper.updateFavorite(info.id, 0);
          new bgRequery().execute();
          return true;
      default:
          return super.onContextItemSelected(item);
      }
}

private class bgRequery extends AsyncTask<Void, Integer, Void> {
    @Override
    protected Void doInBackground(Void... voids ) {
        mSpellCursor = fetchCursor();
        return null;
    }

    @Override
    protected void onPostExecute(Void voids) {
        spellsAdapter.changeCursor(mSpellCursor);
    }
}

Here is the exception:

D/SpellBook( 5362): fetching Cursor
E/AndroidRuntime( 5362): FATAL
EXCEPTION: background thread
E/AndroidRuntime( 5362):
android.view.ViewRoot$CalledFromWrongThreadException:
Only the original thread that created
a view hierarchy can touch its views.
E/AndroidRuntime( 5362): at
android.view.ViewRoot.checkThread(ViewRoot.java:2932)
E/AndroidRuntime( 5362): at
android.view.ViewRoot.requestLayout(ViewRoot.java:629)
E/AndroidRuntime( 5362): at
android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime( 5362): at
android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime( 5362): at
android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime( 5362): at
android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime( 5362): at
android.widget.AbsListView.requestLayout(AbsListView.java:1102)
E/AndroidRuntime( 5362): at
android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:790)
E/AndroidRuntime( 5362): at
android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)
E/AndroidRuntime( 5362): at
android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
E/AndroidRuntime( 5362): at
android.widget.CursorAdapter.changeCursor(CursorAdapter.java:260)
E/AndroidRuntime( 5362): at
com.zalzala.spellbookpf.SpellsAz$bgRequery.onPostExecute(SpellsAz.java:228)
E/AndroidRuntime( 5362): at
com.zalzala.spellbookpf.SpellsAz$bgRequery.onPostExecute(SpellsAz.java:1)
E/AndroidRuntime( 5362): at
android.os.AsyncTask.finish(AsyncTask.java:417)

So the error occurs when spellsAdapter.changeCursor(mSpellCursor); is called in onPostExecute. Since that function runs in the ui thread, I’m having a hard time understanding why I’m getting a background Tread error. What’s making this harder to understand and debug is that it really only happens the very first time I do this. Every other time it works fine, even when the app starts fresh or the phone has rebooted. The only way to reproduce the bug is to uninstall the app and install it fresh.

Just in case anyone needs it, I’m including the code of my adapter:

public class SpellListAdapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private Context mContext;
public SpellListAdapter(Context context, Cursor c) {
    super(context, c);
    mContext = context;
    mLayoutInflater = LayoutInflater.from(context); 
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View v = mLayoutInflater.inflate(R.layout.list_item_fave, parent, false);
    return v;
}

@Override
public void bindView(View v, Context context, Cursor c) {
    String spell = c.getString(c.getColumnIndexOrThrow(SpellDbAdapter.KEY_SPELL));
    int fave = c.getInt(c.getColumnIndexOrThrow(SpellDbAdapter.KEY_FAVORITE));

    /**
     * Next set the title of the entry.
     */

    TextView Spell = (TextView) v.findViewById(R.id.text);
    if (Spell != null) {
        Spell.setText(spell);
    }

    //Set Fave Icon
    TextView Fave = (TextView) v.findViewById(R.id.fave_icon);
    Fave.setVisibility(View.INVISIBLE);
    if (fave == 1){
        Fave.setVisibility(View.VISIBLE);
    }
}

public void update() {
    notifyDataSetChanged();
}
}

Thanks for your help.

EDIT:
I can work around this issue by not calling the cursor in a separate thread, but I thought it was good practice to use another thread for it instead of using the ui thread, so I would still love some help figuring this out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:51:04+00:00Added an answer on May 27, 2026 at 1:51 pm

    The async task may overflow sometimes. You’d better use you own thread in the onContentChanged of your CursorAdapter. Here is my code for your reference:

    private class NearbyLocationListAdapter extends CursorAdapter
    {
        private static final String TAG = "NearbyLocationListAdapter";
    
        private NearbyLocationActivity mActivity = null;
        private Thread mContentChanageHanderThread = null;
    
        public NearbyLocationListAdapter(NearbyLocationActivity activity, Cursor c) {
            super(activity, c, false);
            this.mActivity = activity;
        }
    
        // called when adding location info into location db
        @Override
        protected void onContentChanged() {
            // AsyncTask will bring RejectedExecutionException, change to use Thread
            // interrupt the original thread since data is updated again
            clearContentChanageHanderThread();
    
            mContentChanageHanderThread = new Thread(new Runnable() {
                // set as synchronized in case called by multiple thread
                @Override
                public synchronized void run() {
                    if (Thread.currentThread().isInterrupted()) {
                        // Log.d(TAG, "Thread interrupted. Aborting.");
                        return;
                    }
    
                    final Cursor cursor = getLocationCursor();
    
                    if (Thread.currentThread().isInterrupted()) {
                        // Log.d(TAG, "Thread interrupted after getting cursor. Aborting.");
                        if (Util.isValid(cursor)) {
                            cursor.close();
                        }
    
                        return;
                    }
    
                    NearbyLocationActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (Util.isValid(cursor)) {
                                // will close the original cursor and switch to new one
                                // notifyDataSetChanged will be called inside
                                // Log.d(TAG, "onContentChanged: will changeCursor");
                                changeCursor(cursor);
                            }
                            else {
                                Log.e(TAG, "onContentChanged: cursor is null or closed");
                            }
                        }
                    });
    
                }
            });
    
            mContentChanageHanderThread.start();
    
        }
    
    .....................................
    public void clearContentChanageHanderThread() {
            //Log.v(TAG,"clearContentChanageHanderThread");
    
            if (mContentChanageHanderThread != null && mContentChanageHanderThread.isAlive()) {
                mContentChanageHanderThread.interrupt();
                mContentChanageHanderThread = null;
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.