0

I need to call a non static method from AsyncTask

This method must be called in onPostExecute

I have used:

Main.mymethod();

But it says:

Cannot make a static reference to the non-static method mymethod() from the type Main

How to fix that?

Thanks

EDIT: I have tried with:

    protected void onPostExecute(String result) {
         new Main().mymethod();
    }

Throws:

07-09 01:43:15.248: E/AndroidRuntime(29945): FATAL EXCEPTION: main
07-09 01:43:15.248: E/AndroidRuntime(29945): java.lang.NullPointerException
07-09 01:43:15.248: E/AndroidRuntime(29945):    at android.app.Activity.findViewById(Activity.java:1794)
07-09 01:43:15.248: E/AndroidRuntime(29945):    at android.os.AsyncTask.finish(AsyncTask.java:602)
07-09 01:43:15.248: E/AndroidRuntime(29945):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
07-09 01:43:15.248: E/AndroidRuntime(29945):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
07-09 01:43:15.248: E/AndroidRuntime(29945):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 01:43:15.248: E/AndroidRuntime(29945):    at android.os.Looper.loop(Looper.java:137)
07-09 01:43:15.248: E/AndroidRuntime(29945):    at android.app.ActivityThread.main(ActivityThread.java:4575)
07-09 01:43:15.248: E/AndroidRuntime(29945):    at java.lang.reflect.Method.invokeNative(Native Method)
07-09 01:43:15.248: E/AndroidRuntime(29945):    at java.lang.reflect.Method.invoke(Method.java:511)
07-09 01:43:15.248: E/AndroidRuntime(29945):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-09 01:43:15.248: E/AndroidRuntime(29945):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-09 01:43:15.248: E/AndroidRuntime(29945):    at dalvik.system.NativeStart.main(Native Method)
user222
  • 191
  • 2
  • 17

3 Answers3

1
mymethod(); call this method simply
mymethod(); you can also pass parameters to it if you have defined parameterized method like mymethod(someObj, someVariable);
Farooq
  • 426
  • 2
  • 12
0

If you have defined the method in the class you are calling you do:

myMethod(); 

Rather than:

Main.myMethod(); 

By placing the class in front of the method (Main.myMethod()), you are calling the method as if it were a static method. Remember, to call a static method you do: ClassName.yourStaticMethod().

Here is an example of how to use AsyncTask and call a method within the class, using the answer I gave you:

    package com.example.async;

import android.R;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;


public class MainActivity extends Activity {
    String something; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_item);
        myTask mt = new myTask(); 
        something = ", something old."; 
        mt.execute(something); 
    }



    public String myMethod(String something){
        Log.d("My Log", "This is something " + something); 

        return something; 
    }

    public class myTask extends AsyncTask<Object, Object, Object> {

        @Override
        protected Object doInBackground(Object... arg0) {
            // TODO Auto-generated method stub
            myMethod(something); 
            return something;
        }

        @Override
        protected void onPostExecute(Object result){
            something = ", something new."; 
            myMethod(something); 
        }
    }
}

Here are the results when it's run on a device, which shows that my method executed:

07-08 18:13:17.900: D/My Log(23241): This is something , something old.
07-08 18:13:17.940: D/memalloc(23241): /dev/pmem: Mapped buffer base:0x5106c000 size:1536000 offset:0 fd:52
07-08 18:13:17.960: D/My Log(23241): This is something , something new.
07-08 18:13:17.980: E/Adreno200-ES20(23241): <qgl2DrvAPI_glUseProgram:1318>: **** 23241: glUseProgram(3)
07-08 18:13:18.010: E/Adreno200-ES20(23241): <qgl2DrvAPI_glUseProgram:1318>: **** 23241: glUseProgram(6)
07-08 18:13:18.020: D/memalloc(23241): /dev/pmem: Mapped buffer base:0x512e3000 size:4812800 offset:3276800 fd:55
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

Without more code its hard to give much of an example but you need to send a reference to your AsyncTask class, assuming its a separate class, and use that reference to call the method.

MyTask myTask = new MyTask(this);

then in your AsyncTask something like

class MyTask extends AsyncTask
{
   MyActivity mActivity;
    public MyTask(Activity act)
{
       mActivity = (MyActivity) act;

      ...

      mActivity . method();  // add this wherever you want to call it
}
codeMagic
  • 44,549
  • 13
  • 77
  • 93