“满纸自怜题素怨,片言谁解诉秋心”
今天在运行安卓程序时,出现如下错误:
找了一圈后发现,是因为我在onCreate方法里写了
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navView = (NavigationView) findViewById(R.id.nav_view);
Log.d("navView",navView.toString());
}
是因为这里的nav_view不是在activity_main.xml里面,所以就会报空指针异常
应该先使用inflate获得另外的xml文件.再使用findViewById方法获取.
View account = View.inflate(this, R.layout.account, null);
navView = (NavigationView)account.findViewById(R.id.nav_view);
Log.d("navView",navView.toString());
这样就OK啦.