2012-07-09 90 views
2

我有一個方法searchPlace()更新在谷歌地圖的類A(FindItOnMap)中的自定義地點對象的static Arrays和更新各種地址的方法updateMap()。 我調用這些方法Button.onClick和所有正常工作。AsyncTask作爲內部類和靜態字段問題

由於這些方法使用互聯網數據,這個操作可能需要一段時間,我一直在尋找類A內的內部類B(YourCustomAsyncTask)的實現,它擴展了AsyncTask以在處理這兩個過程中顯示等待對話方法

的用戶建議這種形式的解決方案(即表面上似乎有效):

public class FindItOnMap extends MapActivity { 
    static Place[] foundResults; 
    private ProgressDialog dialog; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.ricerca_condominio); 
     mapView = (MapView)findViewById(R.id.mapView); 
      ........... 

     ((ImageButton) findViewById(R.id.btSearch)).setOnClickListener(mSearchListenerListener); 
    } 

    OnClickListener mSearchListener = new OnClickListener() { 
     public void onClick(View v) { 
      String location=editorLocation.getText().toString(); 
      String name=editorName.getText().toString(); 
      //Call the AsyncTask here 
      new YourCustomAsyncTask().execute(new String[] {name, location}); 
     } 
    }; 


    private class YourCustomAsyncTask extends AsyncTask <String, Void, Void> { 
     @Override 
     protected void onPreExecute() { 
      dialog = new ProgressDialog(Main.this); 
      dialog.setMessage("Loading...."); 
      dialog.setIndeterminate(true); 
      dialog.setCancelable(true); 
      dialog.show(); //Maybe you should call it in ruinOnUIThread in doInBackGround as suggested from a previous answer 
     } 

     @Override 
     protected Void doInBackground(String... strings) { 
      try { 
       search(strings[0], string[1]); 
       return null; 
      } catch(Exception e) { 
      } 
     } 

     @Override 
     protected void onPostExecute(Void params) { 
      updateMapWithResult(); 
      dialog.dismiss(); 
      //result 
     } 
..... 
    } 

等待的對話框顯示和方法在後臺調用, 但是,對於一些奇怪的原因靜態列表foundResults結果填充各種null項目...

這怎麼可能?

如果我調用內部類之外的方法搜索(位置,名稱)都可以正常工作,並且updateMapWithResult();更新所有的地址,所以這兩個方法都可以。只有當我試圖在內部類中調用這個json調用似乎正在工作,但靜態變量foundResults填充了null元素和程序無法正常工作。

有什麼建議嗎?

+0

是否有'foundResults'爲'static'的具體原因?你的內部類應該能夠訪問它,而不是'靜態'...不知道這是否能解決你的問題,但值得一試。 – 2012-07-09 23:22:58

+0

是的,是靜態的,因爲在其他各種靜態程序中使用 – Asgard 2012-07-09 23:25:31

回答

2

我明白問題在哪裏。 您必須在UI線程上運行搜索方法。

所以更改此代碼塊:

@Override 
     protected Void doInBackground(String... strings) { 
      try { 

       search(strings[0], string[1]); 
       return null; 

      } catch(Exception e) { 
      } 

     } 

與此

@Override 
     protected Void doInBackground(final String... strings) { 
      try { 
       runOnUiThread(new Runnable() { 
       public void run() { 
        search(strings[0], string[1]); 
        return null; 
        } 
       }); 

      } catch(Exception e) { 
       e.printStackTrace(); 
      } 
     } 

而且都應該正常工作。

+0

謝謝,這個作品! – Asgard 2012-07-10 16:07:49

0

這裏有一個問題:

OnClickListener mSearchListener = new OnClickListener() { 
     public void onClick(View v) { 
       String Location=editorLocation.getText().toString(); 
       String name=editorName.getText().toString(); 
      //Call the AsyncTask here 
      new YourCustomAsyncTask().execute(new String[] {name, location}); 

     } 

Location應該是location

另外這裏:

@Override 
     protected Void doInBackground(String... strings) { 
      try { 

       search(strings[0], string[1]); 

      } catch(Exception e) { 
      } 

     } 

    @Override 
    protected void onPostExecute(Void params) { 
     updateMapWithResult(); 
     dialog.dismiss(); 
     //result 

    } 

doInBackground您搜索後,你不分配一個值。你可以試試這個:

@Override 
     protected Void doInBackground(String... strings) { 
      try { 

       search(strings[0], string[1]); 
       String name = string[0]; 
       String location = string[1] 

      } catch(Exception e) { 
      } 

     } 

或其他什麼東西會在運行時分配值。事實上,它似乎只是搜索,然後沒有別的。

原因foundResultsnull是因爲你永遠不會給它賦值。

+0

位置是一個轉換錯誤,是我的程序中的位置, 搜索是一個無效的靜態方法,用一個值更新類的靜態字段。 。爲什麼我必須明確賦值?我如何做到這一點? – Asgard 2012-07-10 07:49:45

0

您的AsyncTask沒有任何問題。請包括search()方法。

+0

搜索方法是一種靜態方法,它用一個值更新該類的靜態字段,並在外部類中工作正常......問題是,當在內部類中調用外部類的靜態字段時正確更新...所以我認爲這並不重要如何實施搜索 – Asgard 2012-07-10 07:45:27