我的gridview在询问子ID时抛出null,我看了类似的问题,但我的视图不能滚动,所以没有重复使用子视图。我希望在gridview出现在屏幕后,几个随机的孩子改变他们的颜色。我已经将代码保持在启动状态,因为在屏幕出现在手机上之后就开始了。我不得不问这个部分代码是应该在开始时还是在恢复时或者在创建方法上保存,以及为什么我会得到空指针错误。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
id1 = Integer.valueOf(intent.getStringExtra("number"));
Log.i("id1bnrgbngbnb", id1.toString());
Drawable d = getResources().getDrawable(R.drawable.images);
height = d.getIntrinsicHeight();
width = d.getIntrinsicWidth();
DisplayMetrics displayMetrics = new DisplayMetrics();
this.getWindowManager()
.getDefaultDisplay()
.getMetrics(displayMetrics);
heightofactivity = displayMetrics.heightPixels;
gridview = (GridView) findViewById(R.id.gridview1);
gridview.setNumColumns(id1);
gridview.setColumnWidth(widthofactivity / id1);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(), "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
protected void onStart() {
super.onStart();
numbers = new ArrayList<>();
int x = numberofboxestoselect(id1);
for (int i = 0; i < x; i++) {
numbers.add(selectrandomnumers(id1));
}
for (int i = 0; i < numbers.size(); i++) {
Log.i("numbers", numbers.toString());
View v = gridview.getChildAt(numbers.get(i));
v.setBackgroundResource(R.drawable.download);
}
}
private int numberofboxestoselect(Integer id1) {
int Min = 0;
int Max = id1 * id1 - 1;
return Min + (int) (Math.random() * ((Max - Min) + 1));
}
记录猫
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundResource(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundResource(int)' on a null object reference
at com.example.utkarsh.finomena.MainActivity.onStart(MainActivity.java:74)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1260)
at android.app.Activity.performStart(Activity.java:6276)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
答案 0 :(得分:0)
int widthofactivity不存在,因此默认为0;因为gridview对象得到了冠,但没有gridview,也没有孩子。我已经使用gridview附加了viewtreeobserver,然后用全局布局方法
编写了代码