ソケット io の runOnUiThread に問題があります。私はこのライブラリを使用しています。私はいくつかのイベントのリスナーを持っています。このイベントが発生したら、イメージを変更します。しかし、イベント画像を取得しても以前のままで、なぜそれが起こったのかわかりません。たぶん、誰かが私が間違っていることを助けたり説明したりします。前もって感謝します。これは私のコードです
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");
}
}