我是编码和热心学习Android Studio的新手,接下来是视频编码。 我已经阅读了那篇文章 - 什么是NullPointerException并且在更多相关文章中理解了这个概念,但是我无法解决虽然我理解错误是关于哪个代码,但如果没有,那么我的问题是什么。 按下计算器应用程序上的任何按钮后,我面临这个NullPointerException:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.blogeomics.calcee.MainActivity.numberPressed(MainActivity.java:167)
at com.blogeomics.calcee.MainActivity$7.onClick(MainActivity.java:97)
代码如下:
package com.blogeomics.calcee;
import android.app.Activity;
import android.media.Image;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import static android.R.string.no;
import static android.os.Build.VERSION_CODES.O;
public class MainActivity extends AppCompatActivity {
String runningNumber = "0";
TextView resultsView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button oneBtn = (Button) findViewById(R.id.button1);
Button twoBtn = (Button) findViewById(R.id.button2);
Button threeBtn = (Button) findViewById(R.id.button3);
Button fourBtn = (Button) findViewById(R.id.button4);
Button fiveBtn = (Button) findViewById(R.id.button5);
Button sixBtn = (Button) findViewById(R.id.button6);
Button sevenBtn = (Button) findViewById(R.id.button7);
Button eightBtn = (Button) findViewById(R.id.button8);
final Button nineBtn = (Button) findViewById(R.id.button9);
Button zeroBtn = (Button) findViewById(R.id.button0);
ImageButton calcBtn = (ImageButton) findViewById(R.id.buttonEqual);
ImageButton divideBtn = (ImageButton) findViewById(R.id.buttonDivide);
ImageButton multiplyBtn = (ImageButton) findViewById(R.id.buttonMultiply);
ImageButton addBtn = (ImageButton) findViewById(R.id.buttonAdd);
ImageButton subtractBtn = (ImageButton) findViewById(R.id.buttonSubtract);
Button clearBtn = (Button) findViewById(R.id.buttonClear);
TextView resultsView = (TextView) findViewById(R.id.result_view);
resultsView.setText("");
oneBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberPressed(1);
}
});
twoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberPressed(2);
}
});
threeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberPressed(3);
}
});
fourBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberPressed(4);
}
});
fiveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberPressed(5);
}
});
sixBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberPressed(6);
}
});
sevenBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberPressed(7);
}
});
eightBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberPressed(8);
}
});
nineBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberPressed(9);
}
});
zeroBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberPressed(0);
}
});
calcBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
subtractBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
multiplyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
divideBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
clearBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
void numberPressed(int number) {
runningNumber = runningNumber + String.valueOf(number);
resultsView.setText(runningNumber);
}
}
我已经阅读了很多有关此错误的帖子,但仍然无法弄清楚如何解决我的问题,我知道这应该是一个小问题,但我无法理解,请帮忙。
答案 0 :(得分:2)
更改此行:
TextView resultsView = (TextView) findViewById(R.id.result_view);
到
resultsView = (TextView) findViewById(R.id.result_view);
现在您的resultsView将是全局的