Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android Java.util.ConcurrentModificationException_Java_Android - Fatal编程技术网

Java android Java.util.ConcurrentModificationException

Java android Java.util.ConcurrentModificationException,java,android,Java,Android,我有一个服务,其中我有一个异步任务 if (s != null) { if (!MainActivity.photoListSend.isEmpty()) { if (MainActivity.photoListSend.size() > 0) { File file = MainActivity.photoListSend.get(0);

我有一个服务,其中我有一个异步任务

if (s != null) {
                    if (!MainActivity.photoListSend.isEmpty()) {
                        if (MainActivity.photoListSend.size() > 0) {
                            File file = MainActivity.photoListSend.get(0);
                            if (file.exists())
                                file.delete();
                            MainActivity.photoListSend.remove(0);
                            Gson gson = new Gson();
                            String jsonCurProduct = gson.toJson(MainActivity.photoListSend);
                            SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("TAG", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedPref.edit();
                            editor.putString("TAG", jsonCurProduct);
                            editor.apply();
                            File imagesFolder = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
                            assert imagesFolder != null;
                        }
                    }

                }
在这一行:

String jsonCurProduct = gson.toJson(MainActivity.photoListSend);
我有java.util.ConcurrentModificationException

这是我的全部课程:

ublic class Sendrer extends Service {

    public static boolean running = false;
    private Timer timer = new Timer();
    private SendPhotoTask asyncSender;
    private Context context;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    public static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        sp = getSharedPreferences("pfref", Activity.MODE_PRIVATE);
        editor = sp.edit();
        Gson gson = new Gson();
        List<File> productFromShared = new ArrayList<>();
        SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("TAG", Context.MODE_PRIVATE);
        String jsonPreferences = sharedPref.getString("TAG", "");

        Type type = new TypeToken<List<File>>() {
        }.getType();
        productFromShared = gson.fromJson(jsonPreferences, type);
        MainActivity.photoListSend = null;
        MainActivity.photoListSend = new ArrayList<>();
        if (productFromShared != null)
            MainActivity.photoListSend.addAll(productFromShared);
        Log.e("tworzenie serwisu ", "tworzenie");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("Dziełanie serwisu ", "Dziełanie");

        if (!running) {
            running = true;
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    asyncSender = new SendPhotoTask();
                    asyncSender.execute();
                }
            }, 1000 * 60 * 2);
        }

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        if (running) {
            timer.cancel();
            asyncSender = new SendPhotoTask();
            asyncSender.cancel(true);
            running = false;
        }
        Log.e("service ", "nie działa");
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

    class SendPhotoTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... strings) {
            running = true;
            if (MainActivity.photoListSend != null) {
                if (!MainActivity.photoListSend.isEmpty())
                    if (NetworkUtil.isNetworkAvailable(context)) {
                        if (MainActivity.photoListSend.size() > 0) {
                            MainActivity.isSend = true;
                            running = true;
                            InputStream responseInputStream = null;
                            Log.e("start wysłania ", "start");
                            try {
                                if (MainActivity.photoListSend.get(0).isFile()) {
                                    responseInputStream = HttpConnectionsUtil.sendPhotoRequest(getApplicationContext(), true, MainActivity.photoListSend.get(0).getName());
                                    if (responseInputStream != null) {
                                        String input = convertStreamToString(responseInputStream);
                                        if (input.equals("empty"))
                                            return "BAD";
                                        else {
                                            try {
                                                int tt = ResponseParser.getType(input);
                                                Log.e("TaG", tt + " ");

                                                if (tt == 0) {
                                                    return null;
                                                } else if (tt == -1) {
                                                    return null;
                                                }
                                            } catch (UnknownAnswerName e) {
                                                e.printStackTrace();
                                                return null;
                                            }
                                        }
                                    }

                                } else {
                                    return "BAD";
                                }

                            } catch (IOException e) {
                                e.printStackTrace();
                                return null;
                            }
//                    Log.e("Wysyłanie zdjęcia ", convertStreamToString(responseInputStream));
                            if (responseInputStream != null)
                                return convertStreamToString(responseInputStream);
                        }
                    }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (NetworkUtil.isNetworkAvailable(context)) {
                if (sp.getBoolean("workOffLine", false)) {
                    editor.putBoolean("workOffLine", false);
                    editor.commit();
                    isConnect.setVisibility(View.VISIBLE);
                    imgIsSend.setVisibility(View.VISIBLE);
                    imgIsNet.setVisibility(View.GONE);
                }
            }
            if (s != null) {
                if (!MainActivity.photoListSend.isEmpty()) {
                    if (MainActivity.photoListSend.size() > 0) {
                        File file = MainActivity.photoListSend.get(0);
                        if (file.exists())
                            file.delete();
                        MainActivity.photoListSend.remove(0);
                        Gson gson = new Gson();
                        String jsonCurProduct = gson.toJson(MainActivity.photoListSend);
                        SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("TAG", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPref.edit();
                        editor.putString("TAG", jsonCurProduct);
                        editor.apply();
                        File imagesFolder = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
                        assert imagesFolder != null;
                    }
                }

            }
            if (s == null) {
                MainActivity.isSend = false;
            }
            if (!MainActivity.photoListSend.isEmpty()) {
                if (NetworkUtil.isNetworkAvailable(context)) {
                    if (MainActivity.photoListSend.size() > 0) {
                        asyncSender = new SendPhotoTask();
                        asyncSender.execute();
                        Log.e("Wysyłanie kolejnego ", "zdjecia");
                    } else {
                        context.stopService(new Intent(context, Sendrer.class));
                        asyncSender.cancel(true);
                        context.startService(new Intent(context, Sendrer.class));
                    }

                } else {
                    context.stopService(new Intent(context, Sendrer.class));
                    asyncSender.cancel(true);
                    context.startService(new Intent(context, Sendrer.class));
                }
            } else {
                MainActivity.isSend = false;
                context.stopService(new Intent(context, Sendrer.class));
                asyncSender.cancel(true);
                context.startService(new Intent(context, Sendrer.class));
            }
            running = false;
        }
    }
}
public类发送者扩展服务{
公共静态布尔运行=false;
专用计时器=新计时器();
私有SendPhotoTask异步发送方;
私人语境;
私人共享参考sp;
私有共享参考。编辑器;
公共静态字符串convertStreamToString(java.io.InputStream is){
java.util.Scanner s=新的java.util.Scanner(is).useDelimiter(“\\A”);
返回s.hasNext()?s.next():“”;
}
@凌驾
public void onCreate(){
super.onCreate();
context=getApplicationContext();
sp=GetSharedReferences(“pfref”,Activity.MODE\u PRIVATE);
editor=sp.edit();
Gson Gson=新的Gson();
List productFromShared=new ArrayList();
SharedReferences SharedReference=getApplicationContext().GetSharedReferences(“标记”,Context.MODE\u PRIVATE);
String jsonPreferences=sharedPref.getString(“标记”,”);
Type Type=new-TypeToken(){
}.getType();
productFromShared=gson.fromJson(jsonPreferences,type);
MainActivity.photoListSend=null;
MainActivity.photoListSend=新建ArrayList();
if(productFromShared!=null)
MainActivity.photoListSend.addAll(productFromShared);
Log.e(“tworzenie serwisu”,“tworzenie”);
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
Log.e(“Dziełanie serwisu”、“Dziełanie”);
如果(!正在运行){
运行=真;
Handler=newhandler();
handler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
asyncSender=新建SendPhotoTask();
asyncSender.execute();
}
}, 1000 * 60 * 2);
}
返回开始时间;
}
@凌驾
公共空间{
如果(正在运行){
timer.cancel();
asyncSender=新建SendPhotoTask();
asyncSender.cancel(true);
运行=错误;
}
Log.e(“服务”,“nie działa”);
super.ondestory();
}
@可空
@凌驾
公共IBinder onBind(意向){
返回null;
}
公共布尔值isMyServiceRunning(类serviceClass){
ActivityManager=(ActivityManager)getSystemService(Context.ACTIVITY_服务);
对于(ActivityManager.RunningServiceInfo服务:manager.getRunningServices(Integer.MAX_值)){
if(serviceClass.getName().equals(service.service.getClassName())){
返回true;
}
}
返回false;
}
类SendPhotoTask扩展了AsyncTask{
@凌驾
受保护的字符串背景(字符串…字符串){
运行=真;
if(MainActivity.photoListSend!=null){
如果(!MainActivity.photoListSend.isEmpty())
if(NetworkUtil.isNetworkAvailable(上下文)){
if(MainActivity.photoListSend.size()>0){
MainActivity.isSend=true;
运行=真;
InputStream响应InputStream=null;
Log.e(“启动wysłania”,“启动”);
试一试{
if(MainActivity.photoListSend.get(0.isFile()){
responseInputStream=HttpConnectionsUtil.sendPhotoRequest(getApplicationContext(),true,MainActivity.photoListSend.get(0.getName());
if(responseInputStream!=null){
字符串输入=convertStreamToString(responseInputStream);
if(input.equals(“空”))
返回“坏”;
否则{
试一试{
int tt=ResponseParser.getType(输入);
Log.e(“TaG”,tt+);
如果(tt==0){
返回null;
}else if(tt==-1){
返回null;
}
}捕获(未知名称e){
e、 printStackTrace();
返回null;
}
}
}
}否则{
返回“坏”;
}
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}
//Log.e(“Wysyłanie zdjęcia”,convertStreamToString(responseInputStream));
if(responseInputStream!=null)
返回convertStreamToString(responseInputStream);
}
}
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
if(NetworkUtil.isNetworkAvailable(上下文)){
if(sp.getBoolean(“workufline”,false)){
编辑器.putBoolean(“workuffline”,false);
commit();
synchronized(MainActivity.this) {
    String jsonCurProduct = gson.toJson(MainActivity.photoListSend);
}