简体   繁体   中英

Getting value from async task class to another class

This is the error

This is my asyncResponse.java

 public interface asyncResponse {

    void processFinish(String output);
}

This is my trialmenu.java where i want to retrieve my value

abstract class trialmenu extends AppCompatActivity implements asyncResponse{
private ImageView logo;
private TextView status;

AsyncStatus asyncTask =new  AsyncStatus();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newmenulayout);

    logo = (ImageView) findViewById(R.id.imglogo);
    status = (TextView) findViewById(R.id.txtStatus);

    AsyncStatus.delegate = this;


}



void processFinish(String output) {
    status.setText(output);


}
}

This is my AsyncStatus.java, I haven't posted my doInBackground for privacy reasons

public class AsyncStatus extends AsyncTask<String, String, StringBuilder> {

public static asyncResponse delegate=null;

@Override
protected void onPostExecute(StringBuilder result) {
    super.onPostExecute(result);

    delegate.processFinish(result.toString());


}
}

You have to register callback to AsyncTask class.

 public class AsyncStatus extends AsyncTask<String, String, StringBuilder> {

    public asyncResponse delegate=null;

    public void setDelegate(asyncResponse delegate){
            this.delegate=delegate;
    }

    @Override
    protected void onPostExecute(StringBuilder result) {
        super.onPostExecute(result);

        delegate.processFinish(result.toString());


    }
}

Do not use a static callback . And you can implement in your calling class by implementing asyncResponse or by Anonymous . And processFinish() implementation has to be public with @Override annotation.

abstract class trialmenu extends AppCompatActivity implements asyncResponse {
    private ImageView logo;
    private TextView status;
    AsyncStatus asyncTask = new AsyncStatus();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newmenulayout);
        logo = (ImageView) findViewById(R.id.imglogo);
        status = (TextView) findViewById(R.id.txtStatus);
        asyncTask.setDelegete(this);

    }

    @Override
    public void processFinish(String output) {
        // Callback
    }
}

Suggestion - Follow naming convention in java to make code more readable.

In Android Studio instead of trying to override the method your-self try and see if Android Studio will do it for you so I'd copy your old method code, then delete. Go to the interface in which you are implementing, then press Alt+Enter, then you should see "Implement Methods". Once you do that there shouldn't be any conflicting errors with your method, let me know if this works :)

Does your class AsyncStatus and class trialmenu in same package? If not, your AsyncStatus cannot access your trialmenu method, or change method processFinish to public.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM