我正在尝试在不同位置创建相同的ImageView 20次,我正在使用此代码:
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)
我不知道为什么它会在for
语句中创建它时返回null,有人可以帮助我,谢谢:)
答案 0 :(得分:4)
确保正确声明相对布局。
您不必获取图像视图数组,因为所有元素都具有相同的位图,因此请定义包含图像视图的另一个xml文件,并且每次将该xml布局视图添加到相对布局!