2013-07-30 29 views
1

我想添加Layout Inflator到我的主視圖,但不添加。和我的應用程序崩潰。在AsyncTask的MainLayout中設置LayoutInflator Android

這裏是我的代碼:

mainLayout = (LinearLayout) findViewById(R.id.user_list); 
     li = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     tempView = li.inflate(R.layout.user_rev_listitem, null); 

私有類MyTask擴展的AsyncTask { 私人語境mContext; private查看rootView;

public MyTask(Context applicationContext, View tempView) { 
     // TODO Auto-generated constructor stub 
     this.mContext=applicationContext; 
     this.rootView=tempView; 

    } 

    @Override 
    protected void onPostExecute(String result) { 


     tareef_nm = (TextView) rootView.findViewById(R.id.tareef_name); 
     tareef_des = (TextView) rootView.findViewById(R.id.tareef); 

     for (int i = 0; i < myList.size(); i++){ 


      JSONObject p = myList.get(i); 



      try { 

       tareef_nm.setText(p.getString(TAG_UserName)); 
       tareef_des.setText(p.getString(TAG_UserReviews)); 



      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 




      mainLayout.addView(tempView); 


     } 



     super.onPostExecute(result); 
     } 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 




      super.onPreExecute(); 
     } 

     @Override 
     protected void onProgressUpdate(Integer... values) { 
      // TODO Auto-generated method stub 
      super.onProgressUpdate(values); 

     } 

     @Override 
     protected String doInBackground(String... params) { 


      String myRespone = null; 
      String url = params[0]; 
      HttpClient client = new DefaultHttpClient(); 

      HttpGet Get = new HttpGet(url); 

      try { 
       HttpResponse response = client.execute(Get); 

       HttpEntity entity = response.getEntity(); 
       myRespone = EntityUtils.toString(entity); 

      } catch (ClientProtocolException e) { 

       e.printStackTrace(); 

       Log.e("My webservice Response", "ClientProtocolException"); 

      } catch (IOException e) { 

       Log.e("My webservice Response", "IOException"); 

       e.printStackTrace(); 
      } 
      JSONObject jsonObj; 

      if (myRespone != null) { 
       try { 



        jsonObj = new JSONObject(myRespone); 
        JSONArray jsonArray = jsonObj.getJSONArray("Results"); 


        for (int i = 0; i <= jsonArray.length() - 1; i++) { 
         myList.add(jsonArray.getJSONObject(i)); 

        } 



       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } else { 
       // do nothing 
      } 
      return null; 
     } 

    } 
+0

顯示日誌貓,並告訴我你在哪裏得到錯誤(錯誤代碼) –

回答

1

您的應用程序崩潰,因爲你是在每次迭代相同tempView增加,到mainLayout。我不明白你想要做什麼。如果你要修復,在每次迭代必須再次誇大它,比如:

的for(int i = 0;我< myList.size();我++){

 tempView = li.inflate(R.layout.user_rev_listitem, null); 
     JSONObject p = myList.get(i); 

.. ..

+0

感謝您的幫助.. – User42590