我试着把一个活动转化成碎片。runOnUiThread
上的错误标记。关于过去:
GoogleActivityV2从活动扩展而来。runOnUiThread在ExecuteTask班。类ExecuteTask嵌套在活动上。
(运行正常)现在:
GoogleActivityV2是从片段扩展而来的。runOnUiThread在ExecuteTask班。类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
}
}
错误说:
如何纠正此错误?
发布于 2013-05-07 17:35:58
试试这个:getActivity().runOnUiThread(new Runnable...
是因为:
1)调用runOnUiThread
中的隐式runOnUiThread
指的是AsyncTask,而不是片段。
2) doesn't have runOnUiThread.
注意,如果您已经在主线程上,Activity
只执行Runnable
,否则它将使用Handler
。在您的片段中的Handler
--如果您不想担心this
的上下文,实际上非常容易:
// 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
中,这已经在主线程上了。
发布于 2020-01-09 00:16:38
使用Kotlin扩展函数
fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
然后,在任何Fragment
中,只需调用runOnUiThread
即可。这使得调用在活动和片段之间保持一致。
runOnUiThread {
// Call your code here
}
注意:如果
Fragment
不再附加到Activity
上,则不会调用回调,也不会引发异常。
如果您想从任何地方访问此样式,可以添加一个公共对象并导入该方法:
object ThreadUtil {
private val handler = Handler(Looper.getMainLooper())
fun runOnUiThread(action: () -> Unit) {
if (Looper.myLooper() != Looper.getMainLooper()) {
handler.post(action)
} else {
action.invoke()
}
}
}
发布于 2020-08-20 10:04:58
对Kotlin来说,只需做这个
activity?.runOnUiThread(Runnable {
//on main thread
})
https://stackoverflow.com/questions/16425146
复制相似问题