【问题标题】:AsyncTask as Inner class and static field issueAsyncTask 作为内部类和静态字段问题
【发布时间】:2012-07-09 23:16:35
【问题描述】:

我有一个方法 searchPlace() 使用谷歌地图更新 A 类 (FindItOnMap) 中的自定义 Place Object 的 static Arrays,以及更新各种地理点的方法 updateMap()。 我调用这些方法Button.onClick 并且一切正常。

由于这些方法使用互联网数据,此操作可能需要一段时间,我一直在寻找扩展 AsyncTask 以在处理过程中显示等待对话框的 A 类内部的内部类 B(YourCustomAsyncTask) 的实现这两种方法中的一种

一位用户以这种形式提出了解决方案(显然似乎有效):

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 项目...

这怎么可能?

如果我在内部类之外调用方法 search(location, name) 一切正常,updateMapWithResult(); 更新所有地理点,所以这两种方法都可以。仅当我尝试在内部类中调用它时,json 调用似乎正在工作,但静态变量 foundResults 填充有 null 元素并且程序无法正常工作。

有什么建议吗?

【问题讨论】:

  • foundResultsstatic 是否有特定原因?您的内部类应该能够在没有 static 的情况下访问它...不确定这是否会解决您的问题,但值得一试。
  • 是的,是静态的,因为在类内的各种其他静态过程中使用

标签: android static android-asynctask inner-classes


【解决方案1】:

我知道问题出在哪里。 您必须在 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();
           } 
        } 

一切都应该正常工作。

【讨论】:

    【解决方案2】:

    这里有一个问题:

    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 的原因是因为你从来没有给它赋值。

    【讨论】:

    • 位置是一个转录错误,是我程序中的位置,搜索是一个无效的静态方法,它用一个值更新类的静态字段......为什么我必须明确赋值?我怎么能这样做?
    【解决方案3】:

    您的AsyncTask 没有任何问题。请包含search() 方法。

    【讨论】:

    • 搜索方法是一个静态方法,它用一个值更新类的静态字段,并且在外部类中工作正常......问题是当在内部类中调用静态字段时外部类的更新不正确......所以我认为如何实现搜索并不重要
    猜你喜欢
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    相关资源
    最近更新 更多