0

我需要从 AsyncTask 调用一个非静态方法

此方法必须在 onPostExecute 中调用

我用过:

Main.mymethod();

但它说:

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

如何解决?

谢谢

编辑:我尝试过:

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

抛出:

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)
4

3 回答 3

1
mymethod(); call this method simply
mymethod(); you can also pass parameters to it if you have defined parameterized method like mymethod(someObj, someVariable);
于 2013-07-09T00:06:08.177 回答
0

如果您已经在您调用的类中定义了该方法,您可以:

myMethod(); 

而不是:

Main.myMethod(); 

通过将类放在方法 ( Main.myMethod()) 的前面,您可以像调用静态方法一样调用该方法。请记住,要调用静态方法,您需要这样做:ClassName.yourStaticMethod().

这是一个如何AsyncTask在类中使用和调用方法的示例,使用我给你的答案:

    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); 
        }
    }
}

以下是它在设备上运行时的结果,这表明我的方法已执行:

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
于 2013-07-08T23:36:24.987 回答
0

如果没有更多代码,很难给出很多示例,但是您需要发送对您的AsyncTask类的引用,假设它是一个单独的类,并使用该引用来调用该方法。

MyTask myTask = new MyTask(this);

然后在你的AsyncTask东西中

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

      ...

      mActivity . method();  // add this wherever you want to call it
}
于 2013-07-08T23:37:15.873 回答