2013-01-09 45 views
0

我正在嘗試使用AsyncTask來完成一些任務。但是當我完成它。它沒有顯示任何類型的語法錯誤。但在運行時顯示如下圖所示的錯誤AsyncTask在runInBackground發出錯誤

我試圖調試它,但可能會得到什麼問題。請幫助

JsonParsingActivity.java

import java.util.ArrayList; 
import java.util.HashMap; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ListActivity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ListAdapter; 
import android.widget.SimpleAdapter; 

public class AndroidJSONParsingActivity extends ListActivity { 

    final String TAG_PRODUCTS = "products"; 
    final String TAG_CID = "cid"; 
    final String TAG_NAME = "name"; 

    JSONArray products = null; 
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 
    // Creating JSON Parser instance 
    JSONParser jParser = new JSONParser(); 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     new Message().execute(this); 
     // The service section 
     startService(new Intent(this, UpdateService.class)); 

     /** 
     * Updating parsed JSON data into ListView 
     * */ 
     ListAdapter adapter = new SimpleAdapter(this, contactList, 
       R.layout.list_item, 
       new String[] { TAG_NAME,}, new int[] { 
         R.id.name}); 

     setListAdapter(adapter); 


    } 

@Override 
    protected void onStart() { 
     // TODO Auto-generated method stub 
     super.onStart(); 
     new Message().execute(this); 

     ListAdapter adapter = new SimpleAdapter(this, contactList, 
       R.layout.list_item, 
       new String[] {TAG_NAME,}, new int[] { 
         R.id.name}); 

     setListAdapter(adapter); 

    } 
    //Belongs to update service 
    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
//  stopService(new Intent(this, UpdateService.class)); 
    } 

    class Message extends AsyncTask<Context, Integer, Long>{ 

     @Override 
     protected Long doInBackground(Context... params) { 
      // TODO Auto-generated method stub 

       String url = "http://ensignweb.com/sandbox/app/comment11.php"; 
          // getting JSON string from URL 
       JSONObject json = jParser.getJSONFromUrl(url); 

       try { 
        // Getting Array of Contacts 
        products = json.getJSONArray(TAG_PRODUCTS); 

        // looping through All Contacts 
        for(int i = products.length()-1; i >=0; i--){ 
         JSONObject c = products.getJSONObject(i); 

         // Storing each json item in variable 
         String cid = c.getString(TAG_CID); 
         String name = c.getString(TAG_NAME); 
         // creating new HashMap 
         HashMap<String, String> map = new HashMap<String, String>(); 

         // adding each child node to HashMap key => value 
         map.put(TAG_CID, cid); 
         map.put(TAG_NAME, name); 

         // adding HashList to ArrayList 
         contactList.add(map); 
         Log.d("value", contactList.toString()); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       return null; 
     } 
    } 

} 

它顯示爲InnerSetException和setException。

+1

嗨你的消息AsynTask應該返回Long也發佈異常 –

+1

也使用asynctask中的適配器。 – mainu

回答

0

使用類別消息

ListAdapter adapter = new SimpleAdapter(this, contactList, 
      R.layout.list_item, 
      new String[] { TAG_NAME,}, new int[] { 
        R.id.name}); 

    setListAdapter(adapter); 
+1

它不允許在裏面製作simpleAdapter。它顯示「構造函數SimpleAdapter(AndroidJSONParsingActivity.Message,ArrayList >,int,String [],int [])未定義」。我是Android – user1897084

+0

((ListView)findViewById(R.id.list))。setAdapter(adapter); 更改爲此 – arnp

0

更改您的AsyncTask像這裏面的這postExecute()(覆蓋方法)方法:

class Message extends AsyncTask<Void, Void, Void>{ 

    @Override 
    protected Void doInBackground(Void... params) { 
     // Your woriking 

     return null;    
    } 

    @Override 
    protected void onPostExecute(Void result) { 
    // TODO Auto-generated method stub 
    super.onPostExecute(result); 

    ListAdapter adapter = new SimpleAdapter(this, contactList, 
      R.layout.list_item, 
      new String[] { TAG_NAME,}, new int[] { 
        R.id.name}); 

    setListAdapter(adapter);   
} 
0

InnerSetException and setException加註到提供讓您設置的結果一個例外。

您必須重寫done()方法並嘗試獲得結果。

@Override 
    protected void done() { 
     try { 
      if (!isCancelled()) get(); 
     } catch (ExecutionException e) { 
      //If Exception occurred, handle it. 

     } catch (InterruptedException e) { 
      throw new AssertionError(e); 
     } 
    }