@Chesterfield25

Как исправить java.lang.ArithmeticException: divide by zero?

Activity

public class Couple extends AppCompatActivity implements View.OnClickListener {
        private AdView adView;
    
        TextView count_txt, quotes_txt;
        CardView back_btn, copy_btn, share_btn, next_btn;
    
        List<String> quotes_list;
        DatabaseReference databaseReference;
        Model myShayari;
        int position =0;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.custom_layout);
    
            Toolbar toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle("Couple");
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
            adView= (AdView) findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            adView.loadAd(adRequest);
    
            count_txt = findViewById(R.id.countTEXT);
            quotes_txt = findViewById(R.id.quotesTEXT);
            back_btn = findViewById(R.id.backBtn);
            copy_btn = findViewById(R.id.copyBtn);
            share_btn = findViewById(R.id.shareBtn);
            next_btn = findViewById(R.id.nextBtn);
    
            back_btn.setOnClickListener(this);
            copy_btn.setOnClickListener(this);
            share_btn.setOnClickListener(this);
            next_btn.setOnClickListener(this);
    
    
            databaseReference = FirebaseDatabase.getInstance().getReference("couple");
            myShayari = new Model();
            quotes_list = new ArrayList<>();
    
            // Event Value Methodes
    
            databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
                        myShayari = dataSnapshot1.getValue(Model.class);
                        if(myShayari != null) {
                            quotes_list.add(myShayari.getTitle());
                        }
                    }
    
                    quotes_txt.setText(quotes_list.get(position));
                    count_txt.setText(position+"/" + quotes_list.size());
                }
    
                @Override
                public void onCancelled(@NonNull DatabaseError error) {
    
                    Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
    
                }
            });
    
    
    
        }
    
        @Override
        public void onClick(View view) {
    
            switch (view.getId()){
                case R.id.backBtn:
                    back();
                    break;
    
                case R.id.copyBtn:
                    copy();
                    break;
    
                case R.id.shareBtn:
                    share();
                    break;
    
                case R.id.nextBtn:
                    next();
                    break;
    
            }
    
        }
    
        @Override
        public boolean onOptionsItemSelected(@NonNull MenuItem item) {
            if (item.getItemId() == android.R.id.home)
                finish();
            return super.onOptionsItemSelected(item);
        }
    
        private void back(){
            if (position>0){
                position = (position -1) % quotes_list.size();
                quotes_txt.setText(quotes_list.get(position));
                count_txt.setText(position + "/" + quotes_list.size());
            }
        }
    
        private void next(){
            position = (position +1) % quotes_list.size();
            quotes_txt.setText(quotes_list.get(position));
            count_txt.setText(position + "/" + quotes_list.size());
        }
    
        private void copy(){
            ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clipData = ClipData.newPlainText("text", quotes_txt.getText());
    
            if (clipboardManager != null){
                clipboardManager.setPrimaryClip(clipData);
            }
            Toast.makeText(getApplicationContext(), "copied", Toast.LENGTH_SHORT).show();
    
        }
    
        private void share(){
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT,quotes_txt.getText());
            startActivity(Intent.createChooser(intent,"share via"));
        }
    
    }


Ошибку показывает в этих строках
а именно next();

case R.id.nextBtn:
            next();
            break;


вторая ошибка position = (position +1) % quotes_list.size();

private void next(){
        position = (position +1) % quotes_list.size();
        quotes_txt.setText(quotes_list.get(position));
        count_txt.setText(position + "/" + quotes_list.size());
    }
  • Вопрос задан
  • 289 просмотров
Решения вопроса 1
sergey-gornostaev
@sergey-gornostaev Куратор тега Java
Седой и строгий
Ошибка возникает потому, что quotes_list пуст. Либо добавьте try-catch, либо проверяйте size на ноль, либо доработайте алгоритм так, чтобы next вызывался только при непустом quotes_list.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
Bell Integrator Ульяновск
До 400 000 ₽
Bell Integrator Хабаровск
До 400 000 ₽
Bell Integrator Ижевск
До 400 000 ₽