2013-03-27 77 views
2

我正在開發一個Android應用程序。現在我想使用一個AsyncTask。它在android 4.1中運行完美,但doInBackground方法不在android 2.3.3中運行。有沒有錯誤logcat ..異步任務未運行2.3.3中的doInBackground

我有一個類,擴展vom AsyncTask。所以我用新的Class()。execute()來啓動它。構造函數總是有效的。

你有什麼想法我可以在android 2.3.3中解決這個問題嗎?

編輯: 我的代碼:

private class TopicList extends AsyncTask<String, Integer, String> { 
    private TopicList(){ 
     Log.e("Constructor","Works"); 
    } 
    @Override 
    protected String doInBackground(String... arg0) { 
     Log.e("InBackground","Works"); 
     new Thread(new Runnable() { 
      public void run() { 
     // Network processes 
       } 
     }).start(); 
     return null; 
    } 
} 

我有目標版本17的minSdkVersion 7.你還需要更多的信息?我使用此代碼執行它:

new TopicList().execute(); 

logcat中的錯誤日誌只顯示構造函數的工作原理。

+1

你需要更具體。有數百個應用程序依賴於運行在薑餅上的AsyncTask。它*做*工作。 – 2013-03-27 14:40:23

+2

你有'AsyncTasks'的錯誤。您不需要在'doInBackground'中啓動一個新的'Thread',因爲它已經在不同的線程上爲您自動完成了。 – Shade 2013-03-27 14:52:20

+0

好的,那是對的。但它不能幫助我解決問題。我應該改變它。 – JavaForAndroid 2013-03-27 14:58:15

回答

5

的AsyncTask API: http://developer.android.com/reference/android/os/AsyncTask.html

閱讀上述的文檔後,很明顯,的AsyncTask應該工作在2.3.3版本細(它是在API 3 =機器人1.5添加)。

你確定正確的參數被傳遞?這裏是一個異步任務它調用一個WebService並顯示在活動的一個TextView結果的一個簡單的例子,我希望這可以幫助您在正確的軌道得到:

private class AsyncTaskWebServiceCaller extends AsyncTask<Void, Void, String> { 
    private ProgressDialog dialog = new ProgressDialog(MainActivity.this); 

    @Override 
    protected void onPreExecute() { 
     dialog.setMessage("Connecting to Web Service..."); 
     dialog.show(); 
    } 

    @Override 
    protected String doInBackground(Void...voids) { 
     //create the envelope for the message 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = false; 

     //get the card (SOAPObject) that will be put into the envelope 
     SoapObject soapObject = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME); 
     envelope.setOutputSoapObject(soapObject); 

     //put something on the card 
     PropertyInfo propertyInfo = new PropertyInfo(); 
     propertyInfo.setType(PropertyInfo.STRING_CLASS); 
     propertyInfo.setName("macAddress"); 
     propertyInfo.setValue(macAddress); 
     soapObject.addProperty(propertyInfo); 

     //call WebService 
     try { 
      HttpTransportSE httpTransportSE = new HttpTransportSE(SOAP_ADDRESS); 
      httpTransportSE.debug = true; 
      httpTransportSE.call(SOAP_ACTION, envelope); 
      Object response = envelope.getResponse(); 
      return response.toString();    
     } catch (Exception e) { 
      String text = "Oops!! App has crashed with exception: "+e.getMessage() + "\n" + "Stack trace below:\n"; 
      StackTraceElement[] stackTrace = e.getStackTrace(); 
      for (StackTraceElement stackTraceElement : stackTrace) { 
       text += stackTraceElement.toString() + "\n"; 
      } 
      return text; 
     } 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 
     TextView textView = (TextView)findViewById(R.id.textView1); 
     textView.setText(result); 
    } 

} 

,並調用的AsyncTask,只是做這:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    new AsyncTaskWebServiceCaller().execute(); 
} 
+0

它現在似乎工作。謝謝=) – JavaForAndroid 2013-03-27 15:17:12

+0

不客氣:-) – 2013-03-27 15:18:44

相關問題