Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从其他片段调用静态异步任务_Android_Static_Android Asynctask - Fatal编程技术网

Android 从其他片段调用静态异步任务

Android 从其他片段调用静态异步任务,android,static,android-asynctask,Android,Static,Android Asynctask,我试图从片段中调用其他活动中的异步任务。我试着用各种方法打电话,但都没用。我只是想知道调用静态AsyncTask的最佳方式。以下是我的异步任务: static class MyAsync extends AsyncTask<Void, Void, Void> { Context context; String username, password; private MyAsync(Context context, String username, String

我试图从片段中调用其他活动中的异步任务。我试着用各种方法打电话,但都没用。我只是想知道调用静态AsyncTask的最佳方式。以下是我的异步任务:

static class MyAsync extends AsyncTask<Void, Void, Void> {
    Context context;
    String username, password;
    private MyAsync(Context context, String username, String password) {
        this.context = context;
        this.username = username;
        this.password = password;
    }
    ProgressDialog dialog;
    private String response;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = ProgressDialog.show(context, "Connecting to Server","Getting Credentials"
                , true);
    }

    @Override
    protected Void doInBackground(Void... arg0) {       
        try {               
                ContentDownload download = new ContentDownload();
                response = download.loginApi(agentId, password);
                 if(response.contains("Success")){
                     if(SettingHelper.getFirstCall(context)){
                         ContentDownload.CallApi(context);
                         SettingHelper.setFirstCall(context, false);
                     }
                     if(SettingHelper.getFirstLaunch(context)){
                         ContentDownload load = new ContentDownload();
                         load.callItemApi(context);
                         load.callActionApi(context);
                         SettingHelper.setFirstLaunch(context, false);
                     }
            }
        } catch (Exception e) {
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if(response.contains("Success")){
         context.startActivity(new Intent(context, AllActivity.class));
        }else{
            Toast.makeText(context, "Got back", Toast.LENGTH_SHORT).show();
        }
    dialog.dismiss();
}}  

但是它的给定错误

如果您想从
片段
中使用该类,则为其提供公共可见性,以及公共构造函数,然后您可以调用它:

new LoginActivity.MyAsync(getActivity())

你叫它完全一样的方式,为什么会有不同?我建议您在尝试编写应用程序之前先学习java。试试这个:
newloginactivity.MyAsync(getActivity()).execute()谢谢..我的错误..谢谢@XaverKapeller
new LoginActivity.MyAsync(getActivity())