我在socket 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);
}
});
}
});
}
EDIT!
套接字连接成功,我可以在日志中看到在线/离线消息。我也调试代码并且不显示错误。完整代码
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");
}
}
答案 0 :(得分:1)
我已经解决了我的问题。我不知道为什么,但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");
}
}