2017-05-14 238 views
0

我有這段代碼,我把它從stackoverflow和有點改變它。該代碼檢索圖庫的內容並將每個圖像路徑放入數組列表中。然後它隨機選擇ArrayList中的一個路徑,並將其作爲ImageView的資源。感謝您的關注。爲什麼我會得到「java.lang.IllegalArgumentException」錯誤?

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) 
+1

請告訴我哪行是** MainActivity.java:53 **? –

+0

正如Mehran所說,突出MainActivity行53 – Zoe

+0

謝謝你所有的答案。我明白了! –

回答

0

錯誤明確指出不能傳遞到Random.nextInt()方法的值小於或等於0。

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

此線路:

int number = random.nextInt(count); 

會給出錯誤,因爲您將0作爲計數。使用nextInt(int)需要大於0的輸入。輸入不能是負數或0

0

問題是來自隨機的nextInt。你的計數可能是零?

0

Random#nextInt(int)預計參數爲正值。這裏不是這種情況。

Source