本文介绍了无法访问" findViewById"在AsyncTask的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的AsyncTask类从PHP文件下载数据。下载后,我想要把这个数据,到不同的TextViews,但我不能使用的方法findViewById。

I'm using an AsyncTask class to download data from a php file. After downloading, I want to put this data, into different TextViews, but I can't use the method findViewById.

问题是,我这样做是由单独的类,而这一切的片段中。

The problem, is that I'm doing this by separate classes, and it all within a fragment.

这是我的code:

public class RecuperarComentarisFoto extends AsyncTask<String, String, String>{
private Context mContext;
[...]
public RecuperarComentarisFoto(Context context){
    this.mContext=context;
}
@Override
protected void onPreExecute() {
    [...]
}

@Override
protected String doInBackground(String... arg0) {
    params.add(new BasicNameValuePair("pid", "1"));
    JSONObject json = jsonParser.makeHttpRequest(url_get_comentaris, "GET", params);
    JSONArray productObj;
    //HERE I RETRIEVE DATA FROM JSON. ITS WORKING OK.

    return null;
}

和在那里我有问题:

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    this.pDialog.dismiss();
    TextView comentariEditText = (TextView) findViewById(R.id.commentsMostrar);
}

我已经试过这样:

I've tried this:

        TextView comentariEditText = (TextView) mContext.findViewById(R.id.commentsMostrar);

但不工作了。

But isn't working too.

请注意,我调用这个AsyncTask的从一个片段。正如你所看到的,我不得不通过)的AsyncTask的由getActivity(检索到的情况下:

Note that I'm calling this AsyncTask from a Fragment. As you can see, I had to pass the context retrieved by getActivity() to the AsyncTask:

public class MyFragmentA extends Fragment {
Context cont;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);

    cont=getActivity();
    new RecuperarComentarisFoto(cont).execute();
    return myFragmentView;
}


}

我应该怎么办?

What should I do?

感谢你。

推荐答案

您通过膨胀欣赏到AsyncTask的是这样的:

Pass your inflated view to the AsyncTask like this :

public class RecuperarComentarisFoto extends AsyncTask<String, String, String>{
private Context mContext;
private View rootView;
[...]
public RecuperarComentarisFoto(Context context, View rootView){
    this.mContext=context;
    this.rootView=rootView;
}
@Override
protected void onPreExecute() {
    [...]
}

@Override
protected String doInBackground(String... arg0) {
    params.add(new BasicNameValuePair("pid", "1"));
    JSONObject json = jsonParser.makeHttpRequest(url_get_comentaris, "GET", params);
    JSONArray productObj;
    //HERE I RETRIEVE DATA FROM JSON. ITS WORKING OK.

    return null;
}
@Override
protected void onPostExecute(String result) {
    //super.onPostExecute(result);  <--GET RID OF THIS (it will screw up on 2.1 devices)
    this.pDialog.dismiss();
    // use rootview to findViewById
    TextView comentariEditText = (TextView) rootView.findViewById(R.id.commentsMostrar);
}

然后调用像这样,

Then call like so,

View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);

cont=getActivity();
new RecuperarComentarisFoto(cont, myFragmentView).execute();

这篇关于无法访问&QUOT; findViewById&QUOT;在AsyncTask的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:08