作为一个新手,没有人带,所以这段时间躺了很多坑,今天在这里记录一下我犯下的一些很傻的错误。一方面给犯同样错误的同学一个借鉴,另一方面也提醒自己。
关键词:app闪退、
java - Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
控件一定要在类中声明,然后在方法中进行绑定!
声明控件
public class MainActivity extends AppCompatActivity {
public TextView textView;//在方法外声明
public Button button ;
public int mCount ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
在方法中绑定控件
public class MainActivity extends AppCompatActivity {
public TextView textView;
public Button button ;
public int mCount ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv_test);
button = (Button) findViewById(R.id.bt_changed);
}
}
因为onCreate负责初始化布局,而在布局未初始化之前控件绑定会出现空指针异常,所以出现闪退。