无法访问异步类片段中的xml元素

时间:2013-07-12 11:43:09

标签: android android-fragments

即使在传递布局参考之后,我也无法在片段的异步类中访问像TextViews这样的xml元素。它在onPostExecute之前工作得非常好。 我无法看到一个观点。

public class Enq extends SherlockFragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView=inflater.inflate(R.layout.activity_single_track,container,false);

    Bundle bundle = this.getArguments();
    query_id = bundle.getString("queryID");
    query_type = bundle.getString("queryType");
    query_subject = bundle.getString("querySubject"); 
    query_name = bundle.getString("queryName");

    //TextView tc =(TextView) rootView.findViewById(R.id.check);
    //tc.setText(query_id);
    Context cont = getActivity();
    new LoadSingleMsg(cont,rootView).execute();

    return rootView;
}

class LoadSingleMsg extends AsyncTask<String, String, String> {

        private Context mContext;
        private View rootView;
        public LoadSingleMsg(Context context, View rootView){
            this.mContext=context;
            this.rootView=rootView;
        }

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Loading message...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting song json and parsing
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            params.add(new BasicNameValuePair("MID",query_id ));


            // getting JSON string from URL
            String json = jsonParser.makeHttpRequest(URL_MSGDET, "POST",
                    params);


            try {               
                JSONObject jObj = new JSONObject(json);
                if(jObj != null){

                    name = jObj.getString(TAG_SNAME);
                    subject = jObj.getString(TAG_SUBJECT);
                    message = jObj.getString(TAG_MESSAGE);                  

                    }
                }
            catch (JSONException e) {
                    e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute() {
            // dismiss the dialog after getting song information
            pDialog.dismiss();

                        System.out.println("nam"+ name);
                        TextView txt_sname = (TextView)rootView.findViewById(R.id.name);
                        TextView txt_message = (TextView) rootView.findViewById(R.id.message);
                        TextView txt_subject = (TextView) rootView.findViewById(R.id.subject);

                        // displaying data in view
                        txt_sname.setText(name);
                        txt_message.setText(Html.fromHtml("<b>Message:</b> " + message));
                        txt_subject.setText(Html.fromHtml("<b>Subject:</b> " + subject));


                        }

            }

1 个答案:

答案 0 :(得分:0)

尝试将调用添加到super作为PostExecute中的第一件事:

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);

    pDialog.dismiss();
...
}