我正在尝试开发一个Q& A应用程序,它有一个活动和多个类,可以改变文本视图,按钮,分数等。我有一个setter和getter类。我的主要活动包含一个名为set的方法,它将每个文本视图,按钮分配给它们相应的ID。当我尝试调用main.set();我收到错误:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.view.View android.view.Window.findViewById(int)' on a null object
reference
如果我将set()方法复制并粘贴到我的MorseFalls类中并在该类中调用set(),我就不会收到错误。这让我相信方法和变量不会传递到其他类。有人可以帮忙吗?我已经被困在这5天以上了,它正在破坏我的头脑。我将为调用set方法的main和类粘贴我的代码:
public class MainActivity extends AppCompatActivity {
public static Model model = new Model();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
set();
beginMorse();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//Which scale to go to next (if true means selected)
public void scale(){
//not relevant
}
//Morse Falls
public void beginMorse() {
// Start intent here
Intent intent = new Intent(this, MorseFalls.class);
intent.putExtra("intVariableName", 0);
startActivity(intent);
}
public void set(){
model.setT((TextView) findViewById(R.id.Question));
model.setA((TextView) findViewById(R.id.AnswerA));
model.setB((TextView) findViewById(R.id.AnswerB));
model.setC((TextView) findViewById(R.id.AnswerC));
model.setD((TextView) findViewById(R.id.AnswerD));
model.setCs((TextView) findViewById(R.id.currentscore));
model.setScore((TextView) findViewById(R.id.Score));
model.setUnticked((TextView) findViewById(R.id.please_answer));
model.setCC((TextView) findViewById(R.id.Answer2C));
model.setDD((TextView) findViewById(R.id.Answer2D));
model.setE((TextView) findViewById(R.id.AnswerE));
model.setF((TextView) findViewById(R.id.AnswerF));
model.setG((TextView) findViewById(R.id.AnswerG));
model.setH((TextView) findViewById(R.id.AnswerH));
model.setMyVib((Vibrator) this.getSystemService(VIBRATOR_SERVICE));
model.setB((Button) findViewById(R.id.button));
model.setNext_test((Button) findViewById(R.id.Next_Test));
model.setR((RadioGroup) findViewById(R.id.radioGroup));;
model.setOldpatient((Button) findViewById(R.id.oldtest));
model.setNewpatient((Button) findViewById(R.id.newtest));
}
}
/**
* Morse Falls scale
*/
public class MorseFalls extends AppCompatActivity{
private Model model = MainActivity.model;
private MainActivity main = new MainActivity();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//passing intent
Intent mIntent = getIntent();
//ERROR LIES HERE
main.set();
//set();
declaration();
model.getCC().setVisibility(View.GONE);
model.getDD().setVisibility(View.GONE);
findViewById(R.id.button).setVisibility(View.VISIBLE);
//Morse Falls question one
questionOne();
PreviousScore = score;
//button listener, when button clicked, produce output on textfield "Score"
model.getButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
model.getMyVib().vibrate(70);
PreviousScore = score;
//Score.setText(String.valueOf(action()));
model.getScore().setText(String.valueOf(score));
model.getCs().setText(String.valueOf(score));
i++;
//If answer not selected display "Please select answer
if (model.getR().getCheckedRadioButtonId() == -1) {
model.getUnticked().setVisibility(View.VISIBLE);
model.getUnticked().setText("Please Select An Answer");
} else {
//Loop through questions
if (i == 1)
questionOne();
if (i == 2)
questionTwo();
if (i == 3)
questionThree();
if (i == 4)
questionFour();
if (i == 5)
questionFive();
if (i == 6)
questionSix();
if (i == 7)
MorseFinish();
}
//For testing purpose keep displaying score after each answer
model.getR().clearCheck();
model.getCs().setText("Current Score: " + String.valueOf(score));
}
});
//set Morse to false then call scalr question which shows which scale to complete next
model.getNext_test().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
model.getMyVib().vibrate(70);
main.Morse = false;
main.scale();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//declaring buttons visible and their text
private void declaration(){
//not relevant
}
//scoring system
public void onRadioButtonClicked(View view) {
//not relevant
}
}
/*
Questions
*/
private void questionOne(){
//not relevant
}
private void questionTwo(){
//not relevant
}
private void questionThree(){
//not relevant
}
private void questionFour(){
//not relevant
}
private void questionFive(){
//not relevant
}
private void questionSix(){
//not relevant
}
//Display Morse Score and show advance button
private void MorseFinish(){
model.getA().setVisibility(View.GONE);
model.getB().setVisibility(View.GONE);
model.getT().setText("Final Morse Score");
model.getNext_test().setVisibility(View.VISIBLE);
model.getScore().setVisibility(View.VISIBLE);
model.getButton().setVisibility(View.VISIBLE);
}
public void set(){
model.setT((TextView) findViewById(R.id.Question));
model.setA((TextView) findViewById(R.id.AnswerA));
model.setB((TextView) findViewById(R.id.AnswerB));
model.setC((TextView) findViewById(R.id.AnswerC));
model.setD((TextView) findViewById(R.id.AnswerD));
model.setCs((TextView) findViewById(R.id.currentscore));
model.setScore((TextView) findViewById(R.id.Score));
model.setUnticked((TextView) findViewById(R.id.please_answer));
model.setCC((TextView) findViewById(R.id.Answer2C));
model.setDD((TextView) findViewById(R.id.Answer2D));
model.setE((TextView) findViewById(R.id.AnswerE));
model.setF((TextView) findViewById(R.id.AnswerF));
model.setG((TextView) findViewById(R.id.AnswerG));
model.setH((TextView) findViewById(R.id.AnswerH));
model.setMyVib((Vibrator) this.getSystemService(VIBRATOR_SERVICE));
//model.setB((Button) findViewById(R.id.button));
model.setNext_test((Button) findViewById(R.id.Next_Test));
model.setR((RadioGroup) findViewById(R.id.radioGroup));
model.setCheckTinetti((CheckBox) findViewById(R.id.checkTinetti));
model.setCheckEfficacy((CheckBox) findViewById(R.id.checkEfficacy));
model.setCheckFRAT((CheckBox) findViewById(R.id.checkFRAT));
model.setCheckMorse((CheckBox) findViewById(R.id.checkMorse));
model.setOldpatient((Button) findViewById(R.id.oldtest));
model.setNewpatient((Button) findViewById(R.id.newtest));
model.setButton((Button) findViewById(R.id.button));
}
}
型号:
/**
* Created by user on 07/03/2016.
*/
public class Model extends AppCompatActivity {
public static Vibrator myVib;
public static TextView T, A, B, C, D, E, F, G, H, CC, DD, cs, score, unticked;
public static Button next_test, button, newpatient, oldpatient;
public static RadioGroup r;
public static CheckBox checkTinetti, checkEfficacy, checkFRAT, checkMorse;
public static Vibrator getMyVib() {
return myVib;
}
public static void setMyVib(Vibrator myVib) {
Model.myVib = myVib;
}
public static TextView getT() {
return T;
}
public static void setT(TextView t) {
T = t;
}
public static TextView getA() {
return A;
}
public static void setA(TextView a) {
A = a;
}
public static TextView getB() {
return B;
}
public static void setB(TextView b) {
B = b;
}
public static TextView getC() {
return C;
}
public static void setC(TextView c) {
C = c;
}
public static TextView getD() {
return D;
}
public static void setD(TextView d) {
D = d;
}
public static TextView getE() {
return E;
}
public static void setE(TextView e) {
E = e;
}
public static TextView getF() {
return F;
}
public static void setF(TextView f) {
F = f;
}
public static TextView getG() {
return G;
}
public static void setG(TextView g) {
G = g;
}
public static TextView getH() {
return H;
}
public static void setH(TextView h) {
H = h;
}
public static TextView getCC() {
return CC;
}
public static void setCC(TextView CC) {
Model.CC = CC;
}
public static TextView getDD() {
return DD;
}
public static void setDD(TextView DD) {
Model.DD = DD;
}
public static TextView getCs() {
return cs;
}
public static void setCs(TextView cs) {
Model.cs = cs;
}
public static TextView getScore() {
return score;
}
public static void setScore(TextView score) {
Model.score = score;
}
public static TextView getUnticked() {
return unticked;
}
public static void setUnticked(TextView unticked) {
Model.unticked = unticked;
}
public static Button getNext_test() {
return next_test;
}
public static void setNext_test(Button next_test) {
Model.next_test = next_test;
}
public static Button getButton() {
return button;
}
public static void setButton(Button button) {
Model.button = button;
}
public static Button getNewpatient() {
return newpatient;
}
public static void setNewpatient(Button newpatient) {
Model.newpatient = newpatient;
}
public static Button getOldpatient() {
return oldpatient;
}
public static void setOldpatient(Button oldpatient) {
Model.oldpatient = oldpatient;
}
public static RadioGroup getR() {
return r;
}
public static void setR(RadioGroup r) {
Model.r = r;
}
public static CheckBox getCheckTinetti() {
return checkTinetti;
}
public static void setCheckTinetti(CheckBox checkTinetti) {
Model.checkTinetti = checkTinetti;
}
public static CheckBox getCheckEfficacy() {
return checkEfficacy;
}
public static void setCheckEfficacy(CheckBox checkEfficacy) {
Model.checkEfficacy = checkEfficacy;
}
public static CheckBox getCheckFRAT() {
return checkFRAT;
}
public static void setCheckFRAT(CheckBox checkFRAT) {
Model.checkFRAT = checkFRAT;
}
public static CheckBox getCheckMorse() {
return checkMorse;
}
public static void setCheckMorse(CheckBox checkMorse) {
Model.checkMorse = checkMorse;
}
}
答案 0 :(得分:0)
我不明白你不想做什么。您不应将一个活动中的观点引用传递给另一个活动,也不应引用视图static
。
活动的生命周期由操作系统处理,并且在对活动调用onDestroy之后,其对象可能被垃圾收集(这就是为什么你得到空指针异常)。
答案 1 :(得分:0)
当您执行findViewById时,必须在setContentView期间膨胀的activity_main.xml文件中定义具有您想要的id的视图。
在您的MorseFalls活动中,您通过新的MainActivity()创建了一个新的MainActivity实例。这只是创建一个Java对象 - 它不会调用onCreate。因此,activity_main.xml没有为您充气,并且无法找到视图。现在,谁负责调用活动的生命周期方法,如onCreate和onDestroy?系统会调用它,而不是你。
在任何情况下,请不要将活动视为仅仅是另一个Java类。它不止于此。每个活动必须在清单中声明,操作系统将处理活动的生命周期(例如,当用户最小化您的应用程序时,会自动调用onPause然后onStop活动)。通常,您不应该在活动之间传递引用以防止内存问题。您甚至不应该通过构造函数实例化活动。
如果要重新使用方法,请将它们放在不从任何活动扩展的单独类中。此外,每个活动都应该有自己的布局文件,因此应该有自己的视图,因此应该没有理由重新使用对完全相同的布局文件进行操作的方法,因此也就是相同的视图。