2015-09-12 92 views
-2

我想創建相同的ImageView在不同地點的20倍,我使用此代碼:ImageView的數組返回null對象

rand = new Random(); 
    ImageView[] imageViewArray = new ImageView[20]; 
    ArrayList<Float> xarray = new ArrayList<>(); 
    ArrayList<Float> yarray = new ArrayList<>(); 
    Display display = getWindowManager().getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 
    layoutwidth = size.x; 
    layoutheight = size.y; 
    Toast.makeText(MainActivity.this, " " + layoutwidth, Toast.LENGTH_SHORT). 
      show(); 

    for (int i = 0; i < 20; i++) { 
     imageViewArray[i] = new ImageView(this); 
     imageViewArray[i].setTag(i); 
     imageViewArray[i].setImageResource(R.mipmap.enemy); 
     randx = rand.nextInt(layoutwidth + 1); 
     randy = rand.nextInt(layoutheight + 1); 
     imageViewArray[i].setX(randx); 
     imageViewArray[i].setY(randy); 
     xarray.add(imageViewArray[i].getX()); 
     yarray.add(imageViewArray[i].getY()); 
     rlt.addView(imageViewArray[i]); 
    } 

最終,在行rlt.addView(imageViewArray[i]);我的應用程序崩潰,並且說,這是一個空的對象,這裏是我的logcat:

09-12 14:15:19.941 20395-20395/? E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: firstappdevelopment.thatdoskilledme, PID: 20395 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{firstappdevelopment.thatdoskilledme/firstappdevelopment.thatdoskilledme.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218) 
      at android.app.ActivityThread.access$1000(ActivityThread.java:198) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:145) 
      at android.app.ActivityThread.main(ActivityThread.java:6837) 
      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:1404) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
      at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference 
      at firstappdevelopment.thatdoskilledme.MainActivity.onCreate(MainActivity.java:56) 
      at android.app.Activity.performCreate(Activity.java:6500) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218) 
            at android.app.ActivityThread.access$1000(ActivityThread.java:198) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:145) 
            at android.app.ActivityThread.main(ActivityThread.java:6837) 
            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:1404) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115) 

我不知道爲什麼它返回null,因爲我創造它的for語句中,任何人都可以請幫我,謝謝:)

+1

什麼是你的代碼? –

+0

@AndroidWeblineindia ohhh ..我從獲得佈局大小切換到獲取屏幕大小和意外刪除行初始化佈局,謝謝oops:P –

回答

4
  1. 確保您正確地聲明相對佈局。

  2. 您不必採用圖像視圖陣列,因爲所有元素都具有相同的位圖,因此定義另一個包含圖像視圖的xml文件,並且每次將xml佈局視圖添加到相對佈局!

+0

好主意,我會嘗試它也許。 –