Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/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_Android Asynctask_Android Syncadapter - Fatal编程技术网

Android 异步任务对话框生成器错误

Android 异步任务对话框生成器错误,android,android-asynctask,android-syncadapter,Android,Android Asynctask,Android Syncadapter,因此,在我的应用程序中,我想实现一个自动升级功能,以便我的应用程序的用户可以轻松升级应用程序。 为了做到这一点,我决定使用syncAdapter从远程mySQL服务器更新本地SQLite。 因此,在我的syncAdapter中,我正在调用我的AsyncTask类来进行应用程序升级,如下所示: public SyncAdapter(Context context, boolean autoInitialize) { super(context, autoInitialize); t

因此,在我的应用程序中,我想实现一个自动升级功能,以便我的应用程序的用户可以轻松升级应用程序。 为了做到这一点,我决定使用syncAdapter从远程mySQL服务器更新本地SQLite。 因此,在我的syncAdapter中,我正在调用我的AsyncTask类来进行应用程序升级,如下所示:

public SyncAdapter(Context context, boolean autoInitialize) {
    super(context, autoInitialize);
    this.context = mContext;
}

@Override
  public void onPerformSync(Account account, Bundle extras, String authority,ContentProviderClient provider, SyncResult syncResult) {
Context mcontext = mContext;
new AsyncGetVersiune(mcontext).execute("3", "1");
...
}
如果需要升级,则我的AsyncGetVersione类会执行以下操作:

public class AsyncGetVersiune extends AsyncTask<String, Integer, Double> {
  private Context mContext;

  public AsyncGetVersiune (Context context){
    super();
    mContext = context;
  }

IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
mContext.registerReceiver(downloadReceiver, filter);
final String calex = calea;

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage("There is a newer version available. Upgrade now?")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    //if the user agrees to upgrade
    public void onClick(DialogInterface dialog, int id) {
        //start downloading the file using the download manager
        downloadManager = (DownloadManager) mContext.getSystemService(mContext.DOWNLOAD_SERVICE);
        Uri Download_Uri = Uri.parse(calex);
        DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
        request.setAllowedOverRoaming(false);
        request.setTitle("Download new version");
        request.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, "Newversion.apk");
        downloadReference = downloadManager.enqueue(request);
        }
     })
.setNegativeButton("Not now", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
    }
 });
builder.create().show();
我能做什么?我想把对话放在适当的地方。。。 因此syncAdapter使用AsyncTask类检查新版本。如果AsyncTask找到新版本,那么在onPostExecute中,我想显示对话框,然后从那里。。。开始下载并安装新版本

但由于我正在从syncAdapter启动升级过程。。。我不能让一个活动纠缠在这里


那么我该怎么办呢?

您应该只在UI方法上调用AlertDialog,例如在onPostExecute上,当您的上下文不再有效时,会发生异常。 尝试传递getApplicationContext而不是getContext或MacActivity.this


您可以从SyncAdapter实例化的位置粘贴代码。

只需传递错误的上下文变量即可。所以需要从这里改变

new AsyncGetVersiune(mcontext).execute("3", "1");

评论这行

Context mcontext = mContext;

它在adapter类中再次创建了它的实例。

即使我放弃AlertDialog并开始下载。。。。我仍然需要调用request.setdestinationnexternalfilesdirmcontext,Environment.DIRECTORY\u DOWNLOADS,Newversion.apk;这就是我的背景。因此,即使没有AlertDialog,仍然会收到此错误。那么解决方案是什么?放弃使用这种方法?为什么每个应用程序都能做到这一点,而我却做不到?请更清楚地描述您的需求-您首先启动AlertDialog,然后启动AsyncTask?反之亦然?在第一种情况下,您不应该将AlertDialog放在AsyncTask中。这似乎是你的错误。您应该启动一个对话框,处理它的结果,如果需要,像往常一样启动AsyncTask。
new AsyncGetVersiune(context).execute("3", "1");
Context mcontext = mContext;