2012-02-26 528 views
1

我使用的動作條福爾摩斯/片段,我的應用程序異步任務

我的問題是,我需要調用一個異步任務中,是一個靜態類

的編譯器的片段運行抱怨「沒有可以訪問類型KeywordSearch的封閉實例」

我想過移動異步類並創建它自己的類,但是在我的KeywordSearch類中聲明的變量太多,用於異步任務

我如何執行異步任務而不崩潰?

我的列表視圖類(片段):

public static class ListFragment extends Fragment { 
    private ListView list_list; 
    private LazyAdapter adp_list; 

    private KeywordSearch.SearchTask run; 
    //run.execute(); 

    //new SearchTask().execute(); 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);    
    } 
    @Override 
    public void onDestroy() 
    { 
     if(!RTN_REQ.equals("SSDR") && data_got){ 
     adp_list.imageLoader.stopThread(); 
      } 
     list_list.setAdapter(null); 
     if(data_got) 
     ImageLoader.clearCache(true,true);  
     super.onDestroy();  
    } 
    @Override 
    public void onPause() 
    { 
     /* 
     if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){ 
      list_list.setNumColumns(2); 
     }else if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      list_list.setNumColumns(3); 
     } 
     */ 
     super.onPause(); 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View v = inflater.inflate(R.layout.list, container, false); 
     adp_list = new LazyAdapter(getActivity(),list_data, 0); 
     run = new SearchTask(); 
     list_list = (ListView) v.findViewById (R.id.list); 
     if(list_data != null){ 

      list_list.setAdapter(adp_list); 
      list_list.setOnItemLongClickListener(new OnItemLongClickListener() { 
       @Override 
       public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {    
        String[] FINAL = list_data[arg2].split("##"); 
        final Intent d = new Intent(); 
        d.putExtra("DATA",FINAL[0]); 
        d.setClass(getActivity().getApplicationContext(), Details.class); 
        startActivity(d); 
        return false; 
        } 
      });   
      list_list.setOnItemClickListener(new OnItemClickListener(){ 
       @Override 
       public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
       { 
        if(!eno_act){ 
         String[] FINAL = list_data[arg2].split("##"); 
         final Intent i = new Intent(Intent.ACTION_VIEW); 
         i.setData(Uri.parse(FINAL[1]));     
         startActivity(i); 
        }else{ 
         String[] FINAL = list_data[arg2].split("##"); 
         level = 1; 
         level_filter = FINAL[2]; 
         run.execute(); //this is where it crashes!! 




        } 

       } 
      });  
     } 
     return v; 
    } 
} 

和我SearchTask類:

class SearchTask extends AsyncTask<Void, Void, String> { 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     //....to long to show.. 
    } 

    @Override 
    protected String doInBackground(Void... unsued) { 
     //....to long to show.. 
    } 

    @Override 
    protected void onProgressUpdate(Void... unsued) { 
     //....to long to show.. 
    } 

    @Override 
    protected void onPostExecute(String sResponse) { 
     //....to long to show.. 
    } 
} 

回答

3

通過你的KeyWordSearch類的這些變量在異步任務類

的構造函數,例如

int foo; 

public SearchTask(int foo) //your variables here 
{ 
    this.foo = foo; 
} 

傳遞數據示例

//在您的異步任務中,您可以在doInBackground或任何其他方法中執行此操作。 讓foo是你的字符串數組

Bundle b = new Bundle(); 
Intent i = new Intent(context, YourActivity.class) 
b.putExtraStringArray("myStringArray", foo); 
i.putExtras(b); 

//您的活動接收這個數組作爲

String[] myArray = this.getIntent().getStringArrayExtra("myStringArray"); //this should be done after executing asyncTask class. 
+0

你的意思是,如果我出我的移動異步任務?所以這就是我將所有的變數傳遞給異步類的權利? – tom91136 2012-02-26 14:54:27

+0

是的,您可以通過這種方式傳遞變量並在asyncTask類中的任何地方使用它們。 – Sunny 2012-02-26 14:56:20

+0

並執行異步類作爲'新的SearchTask(你的變量在這裏).execute();' – Sunny 2012-02-26 14:57:42