0

I'm trying to do a twitter android application. I'm still working on the login. So I'm using asynctaskloader after a friend suggested me to use it. I believe I get a null pointer exception at this line:

this.consumer = (OAuthConsumer) new getCommonsHttpOAuthConsumer(context);

here's my asynctaskloader class:

class getCommonsHttpOAuthConsumer extends AsyncTaskLoader{

public getCommonsHttpOAuthConsumer(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

@Override
public OAuthConsumer loadInBackground() {
    // TODO Auto-generated method stub

    return new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
}

}

what am I doing wrong? do you guys need to see more code? thank you.

Jello
  • 551
  • 1
  • 5
  • 12

1 Answers1

0

You're not using the loader class right.

You need to call the LoaderManager with this line:

    getLoaderManager().initLoader(ID_FOR_THIS_LOADER, DATA_BUNDLE, CALLBACK);

If you're in a Fragment you need to add getActivity() at the beginning, and if you are using the android.support.v4.jar, you will call getSupportLoaderManager().

You place this line in your onCreate or onResume method. It will simply notify your activity that you want to start a new loader.

After that you'll need to implement the callbacks notifying that your loader is created/finished. This callbacks are implemented by the object you specified as third parameter (CALLBACK). It can be an activity, a fragment... You will find the syntax online.

Here is what it will look like:

    // Callback called by your Activity
    @Override
    public Loader<OAuthConsumer> onCreateLoader(int id, Bundle arg1) {
        loader = new getCommonsHttpOAuthConsumer();
        return loader;
            // After this method you're going in loadInBackground()
    }

    @Override
    public void onLoadFinished(Loader<OAuthConsumer> loader, OAuthConsumer pl) {
           // After loadInBackground() you arrive here, with your new object OAuthConsumer
           this.consumer = pl;
    }

It should work like this, hope it helps!

Pipo
  • 1