조각의 runOnUiThread
활동을 조각으로 변환하려고합니다. 의 오류 표시 runOnUiThread
. 과거에 :
GoogleActivityV2는 활동에서 확장됩니다. ExecuteTask 클래스의 runOnUiThread. 활동에 중첩 된 ExecuteTask 클래스.
(확인 실행) 지금 :
GoogleActivityV2는 Fragment에서 확장됩니다. ExecuteTask 클래스의 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
}
}
오류 내용 :
이 오류를 어떻게 해결할 수 있습니까?
이 시도: getActivity().runOnUiThread(new Runnable...
왜냐하면:
1) this
호출 의 암시 는 runOnUiThread
조각이 아닌 AsyncTask를 참조하는 것입니다.
2) Fragment
runOnUiThread가 없습니다.
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.
EDIT: @rciovati is right, you are in onPostExecute
, that's already on the main thread.
In Xamarin.Android
For Fragment:
this.Activity.RunOnUiThread(() => { yourtextbox.Text="Hello"; });
For Activity:
RunOnUiThread(() => { yourtextbox.Text="Hello"; });
Happy coding :-)
I used this for getting Date and Time in a fragment.
private Handler mHandler = new Handler(Looper.getMainLooper());
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_head_screen, container, false);
dateTextView = root.findViewById(R.id.dateView);
hourTv = root.findViewById(R.id.hourView);
Thread thread = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
mHandler.post(new Runnable() {
@Override
public void run() {
//Calendario para obtener fecha & hora
Date currentTime = Calendar.getInstance().getTime();
SimpleDateFormat date_sdf = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat hour_sdf = new SimpleDateFormat("HH:mm a");
String currentDate = date_sdf.format(currentTime);
String currentHour = hour_sdf.format(currentTime);
dateTextView.setText(currentDate);
hourTv.setText(currentHour);
}
});
}
} catch (InterruptedException e) {
Log.v("InterruptedException", e.getMessage());
}
}
};
}
참고URL : https://stackoverflow.com/questions/16425146/runonuithread-in-fragment
'developer tip' 카테고리의 다른 글
ggplot2에서 범례 항목 사이의 간격을 변경하는 방법이 있습니까? (0) | 2020.08.11 |
---|---|
Django URL TypeError : view는 callable이거나 include ()의 경우 목록 / 튜플이어야합니다. (0) | 2020.08.11 |
PHP artisan.php serve 용 포트 설정 (0) | 2020.08.11 |
Windows에서 실행되는 Emacs 용 .emacs 파일은 어디에서 찾을 수 있습니까? (0) | 2020.08.11 |
JavaScript에서 null이거나 정의되지 않은 경우 값을 대체합니다. (0) | 2020.08.11 |