从AsyncTask类调用Main类的非静态方法

时间:2013-07-08 23:33:41

标签: java android

我需要从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)

3 个答案:

答案 0 :(得分:1)

mymethod(); call this method simply
mymethod(); you can also pass parameters to it if you have defined parameterized method like mymethod(someObj, someVariable);

答案 1 :(得分: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

答案 2 :(得分: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
}