问题描述
我正在尝试将活动转换为片段.runOnUiThread
上的错误标记.过去:
I'm trying to convert an Activity to fragment. The error mark on runOnUiThread
.on the past:
GoogleActivityV2 从 Activity 扩展而来.类中的 runOnUiThread执行任务.嵌套在活动上的 ExecuteTask 类.
(运行正常)现在:
GoogleActivityV2 从 Fragment 扩展而来.类中的 runOnUiThread执行任务.类 ExecuteTask 嵌套在活动上.(错误在runOnUiThread)
这是我的代码
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
}
}
错误说:
我该如何解决这个错误?
how can I fix this error?
推荐答案
试试这个:getActivity().runOnUiThread(new Runnable...
这是因为:
1) 您对 runOnUiThread
的调用中隐含的 this
指的是 AsyncTask,而不是您的片段.
1) the implicit this
in your call to runOnUiThread
is referring to AsyncTask, not your fragment.
2) Fragment
doesn't have runOnUiThread.
请注意,如果您已经在主线程上,Activity
只会执行 Runnable
,否则它使用 Handler
.你可以在你的片段中实现一个 Handler
如果你不想担心this
的上下文,其实很简单:
Note that Activity
just executes the Runnable
if you're already on the main thread, otherwise it uses a Handler
. 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:
// A class instance
private Handler mHandler = new Handler(Looper.getMainLooper());
// anywhere else in your code
mHandler.post(<your runnable>);
// ^ this will always be run on the next run loop on the main thread.
@rciovati 是对的,您在 onPostExecute
中,它已经在主线程上.
@rciovati is right, you are in onPostExecute
, that's already on the main thread.
这篇关于片段中的 runOnUiThread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!