0

我想在我ListView的服务器中显示一些数据asyncTask。连接和JsonParser工作正常,但是OnPostExecute()当我尝试将数据放入ListView.

所以,这是调用的类asynctask

public class GetInfo extends Activity{ 

    ListView listeView;

    protected void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);    
        setContentView(R.layout.listdata);   
        listeView = (ListView) findViewById(R.id.maListeView);     

        GetJson getJson = new GetJson(GetInfo.this);
        getJson.execute();           
    }
}

这里没有问题。这是我的课AsyncTask

class GetJson extends AsyncTask<Void, Integer,  ArrayList<String>> {

    private Context Mycontext;
    private ListView listview; 
    private ArrayAdapter<String> arrayadapter;
    private ProgressDialog pDialog;
    private ArrayList<String> donnees;
    private ListActivity listAct;

    public GetJson(GetInfo getInfo) {
        Mycontext = getInfo;
    }

    @Override
    protected void onPreExecute() {
        // Showing progress dialog before sending http request
        this.pDialog  = new ProgressDialog(Mycontext);
        this.pDialog.setMessage("Please wait..");
        this.pDialog.setIndeterminate(true);
        this.pDialog.setCancelable(false);
        this.pDialog.show();    
    }

    @Override
    protected  ArrayList<String> doInBackground(Void... arg0) {          
        String result = null;
        InputStream is = null;
        JSONObject json_data=null;
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        this.donnees = new ArrayList<String>();

        try{
            //commmand http
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://192.168.0.100/timesync.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }
        catch(Exception e){
            Log.i("taghttppost",""+e.toString());           
        }

        //parse response
        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));         
            StringBuilder sb  = new StringBuilder();           
            String line = null;            
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }

            is.close();

            result = sb.toString();
        }
        catch(Exception e)
        {
            Log.i("tagconvertstr",""+e.toString());
        }        

        //get json data
        try{
            JSONArray jArray = new JSONArray(result);             
            for(int i=0;i<jArray.length();i++)
            {             
                json_data = jArray.getJSONObject(i);
                this.donnees.add("date :"+ json_data.getString("time") + " ou " +
                    json_data.getString("date") + json_data.getString("hour"));     
            }
        }
        catch(JSONException e){
            Log.i("tagjsonexp",""+e.toString());
        } catch (ParseException e) {
            Log.i("tagjsonpars",""+e.toString());
        }
        return this.donnees;
    }

    @Override
    protected void onPostExecute(ArrayList<String> donnees) {    
        super.onPostExecute(donnees);

        this.pDialog.dismiss();

        for(int i=0;i<donnees.size();i++)
        {             
            Log.i("time", donnees.get(i));
        } 

        // this.arrayadapter = new ArrayAdapter<String>(Mycontext, android.R.layout.simple_list_item_1, this.donnees);
        // this.listview.setAdapter(this.arrayadapter);
    }
}

我的问题是注释行:

// this.arrayadapter = new ArrayAdapter<String>(Mycontext, android.R.layout.simple_list_item_1, this.donnees);
// this.listview.setAdapter(this.arrayadapter);

我不明白为什么...

这是我的错误:

11-05 16:43:36.560: E/AndroidRuntime(11200): FATAL EXCEPTION: main
11-05 16:43:36.560: E/AndroidRuntime(11200): java.lang.NullPointerException
11-05 16:43:36.560: E/AndroidRuntime(11200):at com.json.GetJson.onPostExecute(Adapter.java:134)
11-05 16:43:36.560: E/AndroidRuntime(11200):at android.os.AsyncTask.finish(AsyncTask.java:631)
11-05 16:43:36.560: E/AndroidRuntime(11200):at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-05 16:43:36.560: E/AndroidRuntime(11200):at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)

.
.
.

结果正在被记录,所以我很确定我的代码被传递到OnPostExecute().

4

5 回答 5

0

如果您正在处理 PostExecute Menthod 上的任何 UI 操作,请尝试以下操作:

Handler refresh = new Handler(Looper.getMainLooper());
        refresh.post(new Runnable() {
            public void run( ){
                             // try UI here .
                            });
于 2013-11-06T08:48:12.990 回答
0

您要使用哪个 ListView?GetInfo 类中的一个还是 GetJson 类中的一个?

如果您想将它用作您在 GetJson 类中声明的那个,则需要对其进行实例化

listeView = (ListView) findViewById(R.id.xxxx);
于 2013-11-06T08:38:41.433 回答
0

从类中删除行private ListView listview;线GetJson。问题是您正在访问类的listview对象GetJson(未初始化)而不是Activity's listview对象。

GetInfo Activity像这样直接访问listview

listview.setAdapter(this.arrayadapter);
于 2013-11-06T08:34:45.543 回答
0

ListView listview未在 中初始化GetJSON。因此你得到NullPointerException.

您可能希望将 asynctask 设为 Activity 的内部类并删除 in 的声明,listview因为GetJSON如果将其设为内部类,则它已被声明为 in 的类成员GetInfo

listeView = (ListView) findViewById(R.id.maListeView)GetInfo.

或者使用接口作为回调并在您的活动类中设置列表视图,例如

如何从 AsyncTask 返回布尔值?

ArrayList<String> donnees在您的情况下,而不是示例中的布尔值。

于 2013-11-06T08:36:43.670 回答
0

问题是您的列表视图未初始化。

步骤 1) 更新AsyncTask构造函数:

public GetJson(GetInfo getInfo, ListView list) {
    Mycontext = getInfo;
    listView = list;
}

步骤 2) 在OnCreate

GetJson getJson = new GetJson(GetInfo.this, listeView);
getJson.execute();
于 2013-11-06T08:38:07.243 回答