gpt4 book ai didi

java - 如何在android studio中调用另一个方法中的许多方法

转载 作者:行者123 更新时间:2023-12-02 01:15:31 25 4
gpt4 key购买 nike

我正在 Android Studio 上创建一个测验应用程序。我需要在用户提交时显示带有用户分数的 toast 。我想在submitOnClick 方法内部调用calculateScore 方法,但calculateScore 方法会使应用程序崩溃。

如果我在提交中调用calculateScore,应用程序就会崩溃,如果我在onClick 上调用它,应用程序就会崩溃。

我的代码:

 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final Button button = findViewById(R.id.submit);
button.setOnClickListener(submitOnClick);

}

final View.OnClickListener submitOnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
calculateScore();

Toast.makeText(MainActivity.this, "Your score is " + score, Toast.LENGTH_LONG).show();
}
};

public void checkQuestion1(View view) {
boolean checked = ((RadioButton) view).isChecked();

// Check which radio button was clicked
switch (view.getId()) {
case R.id.right:
if (checked)
score += 1;
break;
case R.id.wrong:
if (checked)
score += 0;
break;
}

}

public void checkQuestion2(View view) {

CheckBox function = findViewById(R.id.checkbox_function);
CheckBox undefined = findViewById(R.id.checkbox_undefined);
CheckBox booleans = findViewById(R.id.checkbox_booleans);

boolean isCheckedFunction = function.isChecked();
boolean isCheckedUndefined = undefined.isChecked();
boolean isCheckedBooleans = booleans.isChecked();

if (isCheckedUndefined && isCheckedBooleans && !isCheckedFunction) {
score += 1;
} else {
score += 0;
}

}

public void checkQuestion3(View view) {

EditText userInput = findViewById(R.id.inputAnswer);
String name = userInput.getText().toString();

if (name.trim().equalsIgnoreCase("Class")) {
score += 1;
} else {
score += 0;
}
}

public void checkQuestion4(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.correct:
if (checked)
score += 1;
break;
case R.id.incorrect:
if (checked)
score += 0;
break;
}

}

public void checkQuestion5(View view) {

CheckBox correctOne = findViewById(R.id.checkbox_correct_one);
CheckBox correctTwo = findViewById(R.id.checkbox_correct_two);
CheckBox incorrectOne = findViewById(R.id.checkbox_incorrect_one);
CheckBox incorrectTwo = findViewById(R.id.checkbox_incorrect_two);
boolean isCheckedCorrectOne = correctOne.isChecked();
boolean isCheckedCorrectTwo = correctTwo.isChecked();
boolean isCheckedIncorrectOne = incorrectOne.isChecked();
boolean isCheckedIncorrectTwo = incorrectTwo.isChecked();

if (isCheckedCorrectOne && isCheckedCorrectTwo && !isCheckedIncorrectOne & !isCheckedIncorrectTwo) {
score += 1;
} else {
score += 0 ;
}

}


public void calculateScore(){
checkQuestion2(null);
checkQuestion1(null );
checkQuestion3(null);
checkQuestion4(null);
checkQuestion5(null);
}

日志猫: --------- 崩溃开始

2019-11-04 10:09:57.514 11886-11886/com.example.android.quizzapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.quizzapp, PID: 11886 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.CompoundButton.isChecked()' on a null object reference at com.example.android.quizzapp.MainActivity.checkQuestion1(MainActivity.java:39) at com.example.android.quizzapp.MainActivity.calculateScore(MainActivity.java:122) at com.example.android.quizzapp.MainActivity$1.onClick(MainActivity.java:32) at android.view.View.performClick(View.java:5610) at android.view.View$PerformClick.run(View.java:22265) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

最佳答案

你打电话

checkQuestion1(null )

使用null,所以它会因NPE而崩溃

关于java - 如何在android studio中调用另一个方法中的许多方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58689315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com