使用Android GridView时出现如下报错:
FATAL EXCEPTION: main
Process: aspectj.lhxtest2, PID: 25329
java.lang.RuntimeException: Unable to start activity ComponentInfo{aspectj.lhxtest2/aspectj.lhxtest2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘void aspectj.lhxtest2.GridView.MyGridView.setAdapter(android.widget.ListAdapter)’ on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
本来以为是布局文件没有读到为空,实际上是因为第一步并不是得到控件。而是得到对象才可调用方法。
源文件报错代码:
MyGridView goodsGridView = (MyGridView) findViewById(R.id.single_choice_view_goods