【问题标题】:Why do I get "java.lang.IllegalArgumentException" error? [duplicate]为什么会出现“java.lang.IllegalArgumentException”错误? [复制]
【发布时间】:2017-05-14 10:51:22
【问题描述】:

我有这段代码,我从 stackoverflow 中获取并稍作改动。该代码检索图库的内容并将每个图像路径放入数组列表中。然后它随机选择 ArrayList 内的路径之一,并将其​​作为 ImageView 的资源。感谢您的关注。

import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    Handler handler = new Handler();
    private ImageView randomPicture;
    private Bitmap currentBitmap = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        randomPicture = (ImageView)findViewById(R.id.random_picture);
        final ArrayList<String> imagesPath = new ArrayList<>();

        String[] projection = new String[]{
                MediaStore.Images.Media.DATA,
        };

        Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        Cursor cur = getContentResolver().query(images, projection, null, null, null)

        if(cur == null) {
            randomPicture.setImageResource(R.drawable.picture);
        }
        else {
            if (cur.moveToFirst()) {

                int dataColumn = cur.getColumnIndex(
                        MediaStore.Images.Media.DATA);
                do {
                    imagesPath.add(cur.getString(dataColumn));
                } while (cur.moveToNext());
            }
            cur.close();
            final Random random = new Random();
            final int count = imagesPath.size();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    int number = random.nextInt(count);
                    String path = imagesPath.get(number);
                    if (currentBitmap != null)
                        currentBitmap.recycle();
                    currentBitmap = BitmapFactory.decodeFile(path);
                    randomPicture.setImageBitmap(currentBitmap);
                    handler.postDelayed(this, 1000);
                }
            });
        }
    }
}

它在光标所在行上崩溃并出现错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.postcards, PID: 2424
java.lang.IllegalArgumentException: n <= 0: 0
at java.util.Random.nextInt(Random.java:182)
at com.example.postcards.MainActivity$1.run(MainActivity.java:53)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

【问题讨论】:

  • 请告诉我哪一行是MainActivity.java:53

标签: java android


【解决方案1】:

Random#nextInt(int) 期望参数为正数。这不是这里的情况。

Source

【讨论】:

    【解决方案2】:

    这一行:

    int number = random.nextInt(count);
    

    给你错误,因为你传递 0 作为计数。使用nextInt(int) 要求输入大于0。输入不能为负或0

    【讨论】:

      【解决方案3】:

      错误清楚地表明您不能将小于或等于 0 的值传递给 Random.nextInt() 方法。

      java.lang.IllegalArgumentException: n <= 0: 0
      

      【讨论】:

        【解决方案4】:

        问题是随机的nextInt。您的计数可能为零?

        【讨论】:

          猜你喜欢
          • 2019-05-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-27
          • 1970-01-01
          • 1970-01-01
          • 2023-03-30
          • 2022-01-18
          相关资源
          最近更新 更多