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

问题描述

我想要一个活动转换为片段。在 runOnUiThread 错误标记。在过去的:

I'm trying to convert an Activity to fragment. The error mark on runOnUiThread.on the past:

GoogleActivityV2从活动延伸。 runOnUiThread类  ExecuteTask。嵌套的活动类ExecuteTask。

(运行正常)现在:

GoogleActivityV2从片段延伸。 runOnUiThread类  ExecuteTask。嵌套的活动类ExecuteTask。 (上的错误  runOnUiThread)

这是我的code

public class GoogleActivityV2 extends SherlockMapFragment implements OnMapClickListener , OnMapLongClickListener , OnCameraChangeListener , TextWatcher {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.activity_googlev2, container, false);
        Init();
        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line);
        textView = (AutoCompleteTextView) getView().findViewById(R.id.autoCompleteTextView1);
        return rootView;
    }

    public void onCameraChange(CameraPosition arg0){
        // TODO Auto-generated method stub
    }

    public void onMapLongClick(LatLng arg0){
        llLoc = arg0;
        stCommand = "onTouchEvent";
        lp = new ExecuteTask();
        lp.execute();
    }

    public void onMapClick(LatLng arg0){
        // TODO Auto-generated method stub
    }

    class ExecuteTask extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute(){
            super.onPreExecute();
            if(stCommand.compareTo("AutoCompleteTextView") != 0) {
                pDialog = new ProgressDialog(getActivity());
                pDialog.setMessage(Html.fromHtml("<b>Search</b><br/>Loading ..."));
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }
        }

        protected String doInBackground(String ... args){
            do something
            return null;
        }

        @Override
        protected void onPostExecute(String file_url){
            if(stCommand.compareTo("AutoCompleteTextView") != 0) pDialog.dismiss();
            runOnUiThread(new Runnable() {
                public void run(){
                    do something
                }
            });
        }
    }
    public void afterTextChanged(Editable s){
        // TODO Auto-generated method stub
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after){
        // TODO Auto-generated method stub
    }

    public void onTextChanged(CharSequence s, int start, int before, int count){
        // TODO Auto-generated method stub
    }
}

错误说:

the error says:

我该如何解决这个问题?

how can I fix this error?

推荐答案

试试这个: getActivity()runOnUiThread(新的Runnable ...

这是因为:

1)隐在调用 runOnUiThread 是指AsyncTask的,不是你的片段。

1) the implicit this in your call to runOnUiThread is referring to AsyncTask, not your fragment.

2)片段没有runOnUiThread

2) Fragment doesn't have runOnUiThread

(<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.2.2_r1/android/app/Fragment.java?av=f">http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.2.2_r1/android/app/Fragment.java?av=f)

然而活动作用:

(<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.2.2_r1/android/app/Activity.java?av=f">http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.2.2_r1/android/app/Activity.java?av=f)

请注意,这只是执行的Runnable 如果您已经在主线程,并使用处理程序如果你不是在主线程。您可以在片段实施处理程序如果你不想担心,它实际上上下文非常简单:

Note that this just executes the Runnable if you're already on the main thread, and uses a Handler if you aren't on the main thread. You can implement a Handler in your fragment if you don't want to worry about the context of this, it's actually very easy:

http://developer.android.com/reference/android/os/Handler.html

编辑:@rciovati是对的,你是在 onPostExecute ,这是已经在主线程

@rciovati is right, you are in onPostExecute, that's already on the main thread.

这篇关于runOnUiThread的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 20:02