我正在創建一個基本的Android應用程序。我創建了一個名爲mathexpresionTV
的TextView
對象,並將其分配給我希望它引用的textview的id值。儘管爲其賦值,但獲得nullpointerexception
TextView mathExpressionTV = (TextView) findViewById(R.id.mathexpressiontextview);
我也有一個按鈕,如果用戶按下它,它會在textview上顯示1。
Button oneButton = (Button) findViewById(R.id.button1)
oneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mathExpressionTV.append("0");
}
});
我將使用mathExpressionTV
通過類,因此我已經delcared它作爲一個全局變量,而不是我的onCreate();
方法,但oneButton
在我onCreate();
聲明。
這是錯誤輸出:Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
在哪個函數中執行此代碼? – 0xDEADC0DE