0

[OK]または[キャンセル]でアラートボックスを表示します。OKを押して非同期タスクを実装したい。Haventは非同期を実行し、しばらくの間それに苦労していました。asychクラスもどこに行くのかわかりません。実行中のメソッドの外に出ますか、それとも外に出ますか?現在のコードは次のとおりです。

 private abstract  class DoAsynchTask extends AsyncTask<Void,Void,Void> 
 {
  protected  void doInBackground()
  {
   Drawable drawable= getImage(imageSelect);     
   MakeWallPaper(drawable,1);
  }

  /* protected void onProgressUpdate(Integer... progress) 
  {
   setProgress(progress[0]);
  }*/

  protected void onPostExecute() 
  {
   Toast.makeText(getApplicationContext(), "Wallpaper Saved.",Toast.LENGTH_LONG).show();      
   AlertDialogProcessing=0;
  }
 }

 public void getWallpaper(final View v)
 {
  if(AlertDialogProcessing==0)
  {  
   final String title="Set Image to Wallpaper";
   final String message="Press OK to set as Wallpaper or CANCEL.\nWait after pushing OK.";
   final String ok="OK";
   final String cancel="CANCEL";
   final AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
   alertbox.setCancelable(true);
   alertbox.setIcon(android.R.drawable.ic_dialog_alert);
   alertbox.setTitle(title);
   alertbox.setMessage(message);
   alertbox.setNegativeButton(cancel, null);
   final AlertDialog dlg = alertbox.create();

   alertbox.setPositiveButton(ok,new DialogInterface.OnClickListener()
   {  
     public void onClick(DialogInterface dlg, int which)
     {  
       DoAsynchTask.execute(null,null,null);  //<<<<Wrong
       dlg.dismiss();
       Vibrate(ClickVibrate); 
     } 
    });
    alertbox.setNegativeButton(cancel,new DialogInterface.OnClickListener(){ public  void onClick(DialogInterface arg0, int arg1){AlertDialogProcessing=0;                     
     Vibrate(ClickVibrate); } });
 alertbox.show();
    }
   }
4

3 に答える 3

2

コードにはいくつかの問題があります。

1)まず第一に、コンパイラはおそらくあなたにこのメッセージを与えています:

タイプMyActivity.DoAsynchTaskは、継承された抽象メソッドAsyncTask.doInBackground(Void ...)MyActivity.javaを実装する必要があります

エラーメッセージをよく見ると、定義したのは次のようになっていることがわかります。

protected  void doInBackground() {

これは必要なことではありません。ばかげているように見えるかもしれませんが、AsyncTaskサブクラスがVoidジェネリックパラメータータイプとして使用する場合、それは次のようになっているdoInBackground()必要があります。

protected Void doInBackground(Void... arg0) {

その(正確な)メソッドを実装していないため、コンパイラは文句を言います。クラスから継承し、その必須/抽象メソッドのすべてabstractを実装できない場合、サブクラスを抽象としてマークすることによってのみコンパイルすることができます。しかし、それは本当にあなたが望むものではありません。

したがって、コードを(abstractクラスから削除)に変更するだけです。

private class DoAsynchTask extends AsyncTask<Void,Void,Void> 

protected Void doInBackground(Void... arg0) {
{
   Drawable drawable= getImage(imageSelect);     
   MakeWallPaper(drawable,1);
   return null;
}

2)そして、他の人が指摘しているように、2番目の問題はあなたがあなたの仕事を次のように始めなければならないということです:

new DoAsynchTask().execute();

いいえ

DoAsynchTask.execute(null,null,null);   

コードが正しいexecute()のは、のstaticメソッドである場合のみですが、そうではAsyncTaskありません。非静的メソッドを呼び出すには、最初にクラスのインスタンスがexecute()必要です。最後に、パラメータリストも必要ありませんが、コードが失敗することはないと思います。newDoAsynchTasknull, null, null

于 2012-07-22T07:33:19.070 に答える
2

doInBackground()はパラメータを指定していないため、パラメータなしで呼び出す必要がありますDoAsynchTask.execute()

なぜあなたのクラスは抽象的ですか?通常、AsyncTaskは、それを開始するアクティビティの内部クラスである必要があります。したがって、アクティビティでダイアログを作成し、[OK]ボタンをクリックしたときにAsyncTaskを実行します。

于 2012-07-21T20:41:16.643 に答える
1

//最終作業コピー-ありがとうALL

public void getWallpaper(final View v)
{
 Vibrate(ClickVibrate); 

 final class SetWallPaperAsynchTask extends AsyncTask<Void,Void,Void> 
 {
  @Override
  protected  Void doInBackground(Void... arg0)
  {
   Drawable drawable= getImage(imageSelect);     
   MakeWallPaper(drawable,1);
   return null;
  }

 @Override
 protected void onPostExecute(Void result) 
 {Toast.makeText(getBaseContext(), "Wallpaper Saved.", Toast.LENGTH_LONG).show();     
  AlertDialogProcessing=0;
 }
}

if(AlertDialogProcessing==0)
{    
 ProgressDialog progress;  
 final String title="Set Image to Wallpaper";
 final String message="Press OK to set as Wallpaper or CANCEL.";
 final String ok="OK";
 final String cancel="CANCEL";

 final AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
 alertbox.setCancelable(true);
 alertbox.setIcon(android.R.drawable.ic_dialog_alert);
 alertbox.setTitle(title);
 alertbox.setMessage(message);
 alertbox.setNegativeButton(cancel, null);
 final AlertDialog dlg = alertbox.create();

 alertbox.setPositiveButton(ok,new DialogInterface.OnClickListener()
 {  
  public void onClick(DialogInterface arg0, int arg1)
  {  
   new SetWallPaperAsynchTask().execute();
   dlg.dismiss();
   Vibrate(ClickVibrate); 
  } 
  });
  alertbox.setNegativeButton(cancel,new DialogInterface.OnClickListener(){ public void  onClick(DialogInterface arg0, int arg1){AlertDialogProcessing=0; Vibrate(ClickVibrate); }   });
  alertbox.show();
  }
 }
于 2012-07-22T14:21:08.077 に答える