我在套接字io中遇到runOnUiThread的问题。我使用这个library。我有一些活动的听众。如果发生此事件,我将更改图像。但是当我得到事件图像时,像以前一样保持不变,我不明白为什么会这样。也许有人帮我解释我在做什么错。提前致谢。这是我的代码
private void online(){
SocketService.getSocket().on("online", new Emitter.Listener() {
@Override
public void call(Object... args) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("myLogs", "online");
onlineOffline.setImageResource(R.drawable.online);
callTo.setOnClickListener(ProfileList.this);
callToRegion.setOnClickListener(ProfileList.this);
}
});
}
});
}
private void offline() {
SocketService.getSocket().on("offline", new Emitter.Listener() {
@Override
public void call(Object... args) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("myLogs", "offline");
onlineOffline.setImageResource(R.drawable.offline);
callTo.setOnClickListener(ProfileList.this);
callToRegion.setOnClickListener(ProfileList.this);
}
});
}
});
}
编辑!
套接字已成功连接,我可以在日志中看到在线/离线消息。我也调试代码,不显示错误。完整代码
public class ProfileList extends Fragment implements View.OnClickListener{
private int pos=0;
private DecodeBitmap decodeBitmap=new DecodeBitmap();
private ArrayList<String> online = new ArrayList<>();
private ImageView onlineOffline;
private RelativeLayout callToRegion;
private Button callTo;
private Activity activity;
static ProfileList newInstance(int position, ArrayList<String> online) {
ProfileList pageFragment = new ProfileList();
Bundle arguments = new Bundle();
arguments.putInt("page", position);
arguments.putStringArrayList("online", online);
pageFragment.setArguments(arguments);
return pageFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pos = getArguments().getInt("page");
online = getArguments().getStringArrayList("online");
Log.d("myLogs", "page pos: " + pos);
activity = getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.profile_list_layout, container, false);
Log.d("myLogs","fragment onCreateView pos "+ pos);
callToRegion = (RelativeLayout) rootView.findViewById(R.id.callToRegion);
callTo = (Button) rootView.findViewById(R.id.callTo);
ImageView mainProfileIcon = (ImageView) rootView.findViewById(R.id.mainProfileIcon);
onlineOffline = (ImageView) rootView.findViewById(R.id.onlineOffline);
if(CallPageFragment.online.get(0).equals("empty")){
new LoadDataFromDatabase("relatives", getActivity(), "");
}
if(LoadDataFromDatabase.relativesData.size()==0){
DialogFragment dlgFragment = new ErrorPopupFragment();
int REQUEST_CODE_DIALOG = 101;
dlgFragment.setTargetFragment(this, REQUEST_CODE_DIALOG);
dlgFragment.setCancelable(true);
String dialogTitle = "error_dialog";
dlgFragment.show(getFragmentManager(), dialogTitle);
activity.onBackPressed();
}
else {
callTo.setText(LoadDataFromDatabase.relativesData.get(pos).getFirstName());
File imgFile = new File(LoadDataFromDatabase.relativesData.get(pos).getAvatar());
if (imgFile.exists()) {
mainProfileIcon.setScaleType(ImageView.ScaleType.CENTER_CROP);
mainProfileIcon.setImageBitmap(decodeBitmap.decodeSampledBitmapFromResource(LoadDataFromDatabase.relativesData.get(pos).getAvatar(), 300, 300));
}
}
Log.d("myLogs", "all relatives online" + online);
Log.d("myLogs", "all relatives" + LoadDataFromDatabase.relativesData.get(pos).getRelativesId());
if(online.size()==0){
onlineOffline.setImageResource(R.drawable.offline);
Log.d("myLogs", "online false");
}
else {
Log.d("myLogs", "online true " + pos + " " + LoadDataFromDatabase.relativesData.get(pos).getRelativesId());
for(int i=0;i<online.size();i++) {
if (LoadDataFromDatabase.relativesData.get(pos).getRelativesId().equals(online.get(i))) {
Log.d("myLogs", "online id " + LoadDataFromDatabase.relativesData.get(pos).getRelativesId() + " " + online.get(i) + " pos: " + pos);
onlineOffline.setImageResource(R.drawable.online);
callTo.setOnClickListener(ProfileList.this);
callToRegion.setOnClickListener(ProfileList.this);
break;
}
else {
onlineOffline.setImageResource(R.drawable.offline);
callTo.setEnabled(false);
}
}
}
try {
MainActivity.textClock.setVisibility(View.VISIBLE);
}
catch(Throwable e) {
Log.d("myLogs", "WTF???!!!!", e);
}
online();
offline();
return rootView;
}
@Override
public void onClick(View v) {
CallPageFragment.currentPosition=pos;
Log.d("myLogs","save currentPosition " + CallPageFragment.currentPosition);
Bundle bundle = new Bundle();
bundle.putString("firstName", LoadDataFromDatabase.relativesData.get(pos).getFirstName());
bundle.putString("avatar", LoadDataFromDatabase.relativesData.get(pos).getAvatar());
bundle.putString("id", LoadDataFromDatabase.relativesData.get(pos).getRelativesId());
Fragment fragment = new CallToFragment();
fragment.setArguments(bundle);
FragmentManager fragmentManager = activity.getFragmentManager();
fragmentManager.beginTransaction().addToBackStack(null).replace(R.id.content_frame, fragment).commit();
}
private void online(){
SocketService.getSocket().on("online", new Emitter.Listener() {
@Override
public void call(Object... args) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("myLogs", "online");
onlineOffline.setImageResource(R.drawable.online);
callTo.setOnClickListener(ProfileList.this);
callToRegion.setOnClickListener(ProfileList.this);
}
});
}
});
}
private void offline() {
SocketService.getSocket().on("offline", new Emitter.Listener() {
@Override
public void call(Object... args) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("myLogs", "offline");
onlineOffline.setImageResource(R.drawable.offline);
callTo.setOnClickListener(ProfileList.this);
callToRegion.setOnClickListener(ProfileList.this);
}
});
}
});
}
@Override
public void onStop() {
super.onStop();
SocketService.getSocket().off("online");
SocketService.getSocket().off("offline");
}
}
最佳答案
我已经解决了我的问题。我不知道为什么,但是runOnUiThread工作正常。我三天试图找到我的代码出了什么问题,然后才起作用。这是完全有效的代码
private void online(){
if (SocketService.getSocket() != null)
SocketService.getSocket().on("online", new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("myLogs", "online");
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("myLogs", "online");
onlineOffline.setImageResource(R.drawable.online);
callToRegion.setOnClickListener(ProfileList.this);
tvHelpDescription.setText("To call " + LoadDataFromDatabase.relativesData.get(pos).getFirstName() + " " +
LoadDataFromDatabase.relativesData.get(pos).getLastName() + " tap here");
CallPageFragment.arrow.setVisibility(View.VISIBLE);
}
});
}
});
else{
Log.e("myLogs", "Socket is null");
}
}
private void offline() {
if(SocketService.getSocket() != null) {
SocketService.getSocket().on("offline", new Emitter.Listener() {
@Override
public void call(Object... args) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("myLogs", "offline");
onlineOffline.setImageResource(R.drawable.offline);
callToRegion.setOnClickListener(null);
tvHelpDescription.setText("You can't call " + LoadDataFromDatabase.relativesData.get(pos).getFirstName() + " " +
LoadDataFromDatabase.relativesData.get(pos).getLastName() + " because she offline");
CallPageFragment.arrow.setVisibility(View.INVISIBLE);
}
});
}
});
}
else{
Log.e("myLogs", "Socket is null");
}
}
关于android - 套接字IO runOnUiThread不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35170544/