Java 为什么在onCreate方法中为小部件分配名称会产生错误,但在另一个方法中分配名称时会起作用?
目前,我有一个onCreate方法,它会立即调用question1方法,该方法分配所有小部件的名称。方法question1中的一切都是有效的,但是当我调用方法question2时,我得到一个NullPointerException 以下是崩溃日志:Java 为什么在onCreate方法中为小部件分配名称会产生错误,但在另一个方法中分配名称时会起作用?,java,android,null,Java,Android,Null,目前,我有一个onCreate方法,它会立即调用question1方法,该方法分配所有小部件的名称。方法question1中的一切都是有效的,但是当我调用方法question2时,我得到一个NullPointerException 以下是崩溃日志: 06-05 18:10:23.468 2223-2223/com.nordicstewdios.presidentreview I/Process﹕ Sending signal. PID: 2223 SIG: 9 06-05 18:11:42
06-05 18:10:23.468 2223-2223/com.nordicstewdios.presidentreview I/Process﹕ Sending signal. PID: 2223 SIG: 9
06-05 18:11:42.705 2291-2306/com.nordicstewdios.presidentreview D/OpenGLRenderer﹕ Render dirty regions requested: true
06-05 18:11:42.708 2291-2291/com.nordicstewdios.presidentreview D/﹕ HostConnection::get() New Host Connection established 0xa68404f0, tid 2291
06-05 18:11:42.748 2291-2291/com.nordicstewdios.presidentreview D/Atlas﹕ Validating map...
06-05 18:11:42.814 2291-2306/com.nordicstewdios.presidentreview D/﹕ HostConnection::get() New Host Connection established 0xa6840590, tid 2306
06-05 18:11:42.846 2291-2306/com.nordicstewdios.presidentreview I/OpenGLRenderer﹕ Initialized EGL, version 1.4
06-05 18:11:42.876 2291-2306/com.nordicstewdios.presidentreview D/OpenGLRenderer﹕ Enabling debug mode 0
06-05 18:11:42.900 2291-2306/com.nordicstewdios.presidentreview W/EGL_emulation﹕ eglSurfaceAttrib not implemented
06-05 18:11:42.900 2291-2306/com.nordicstewdios.presidentreview W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa682fe20, error=EGL_SUCCESS
06-05 18:11:43.172 2291-2303/com.nordicstewdios.presidentreview I/art﹕ Background partial concurrent mark sweep GC freed 411(66KB) AllocSpace objects, 0(0B) LOS objects, 52% free, 922KB/1946KB, paused 11.415ms total 57.104ms
06-05 18:11:46.795 2291-2303/com.nordicstewdios.presidentreview I/art﹕ Background sticky concurrent mark sweep GC freed 1071(71KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 2MB/2MB, paused 8.748ms total 63.358ms
06-05 18:11:46.944 2291-2306/com.nordicstewdios.presidentreview W/EGL_emulation﹕ eglSurfaceAttrib not implemented
06-05 18:11:46.944 2291-2306/com.nordicstewdios.presidentreview W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5cf5480, error=EGL_SUCCESS
06-05 18:11:51.743 2291-2291/com.nordicstewdios.presidentreview I/Choreographer﹕ Skipped 283 frames! The application may be doing too much work on its main thread.
06-05 18:11:58.298 2291-2291/com.nordicstewdios.presidentreview D/AndroidRuntime﹕ Shutting down VM
06-05 18:11:58.298 2291-2291/com.nordicstewdios.presidentreview E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.nordicstewdios.presidentreview, PID: 2291
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.RadioButton.isChecked()' on a null object reference
at com.nordicstewdios.presidentreview.QuizActivity$1.onClick(QuizActivity.java:55)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
06-05 18:12:00.192 2291-2291/com.nordicstewdios.presidentreview I/Process﹕ Sending signal. PID: 2291 SIG: 9
这是我的密码:
package com.nordicstewdios.presidentreview;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.TextView;
import android.widget.RadioButton;
import android.widget.Button;
import org.w3c.dom.Text;
public class QuizActivity extends ActionBarActivity {
private int question = 1;
private int answersWrong = 0;
private TextView textViewQuestion;
private TextView textViewWrong;
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
private RadioButton radioButton4;
private Button buttonNextQ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
question1();
}
private void question1(){
final TextView textViewQuestion = (TextView) findViewById(R.id.textViewQuestion);
final TextView textViewWrong = (TextView) findViewById(R.id.textViewWrong);
final RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
final RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
final RadioButton radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
final RadioButton radioButton4 = (RadioButton) findViewById(R.id.radioButton4);
final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
final Button buttonNextQ = (Button) findViewById(R.id.buttonNextQ);
buttonNextQ.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
if(!radioButton1.isChecked() && !radioButton2.isChecked() && !radioButton3.isChecked() && !radioButton4.isChecked()){
Toast.makeText(QuizActivity.this, "Choose an option before continuing.", Toast.LENGTH_SHORT).show();
}
else if(radioButton1.isChecked()){
Toast.makeText(QuizActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
question++;
question2();
}
else{
Toast.makeText(QuizActivity.this, "Wrong Answer!", Toast.LENGTH_SHORT).show();
answersWrong++;
textViewWrong.setText("Answers Wrong: " + answersWrong);
}
}
});
}
private void question2(){
textViewQuestion.setText("What was the second president, and what were his terms?");
radioButton1.setText("John Adams, 1797 - 1805");
radioButton2.setText("Thomas Jefferson, 1797 - 1801");
radioButton3.setText("Thomas Jefferson, 1797 - 1805");
radioButton4.setText("John Adams, 1797 - 1801");
buttonNextQ.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
if(!radioButton1.isChecked() && !radioButton2.isChecked() && !radioButton3.isChecked() && !radioButton4.isChecked()){
Toast.makeText(QuizActivity.this, "Choose an option before continuing.", Toast.LENGTH_SHORT).show();
}
else if(radioButton1.isChecked()){
Toast.makeText(QuizActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
question++;
//question3();
}
else{
Toast.makeText(QuizActivity.this, "Wrong Answer!", Toast.LENGTH_SHORT).show();
answersWrong++;
textViewWrong.setText("Answers Wrong: " + answersWrong);
}
}
});
}
private void question3(){
}
}
因为您要在question2方法中再次声明所有TextView和单选按钮
private void question1(){
textViewQuestion = (TextView) findViewById(R.id.textViewQuestion);
textViewWrong = (TextView) findViewById(R.id.textViewWrong);
radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
radioButton4 = (RadioButton) findViewById(R.id.radioButton4);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
buttonNextQ = (Button) findViewById(R.id.buttonNextQ);
buttonNextQ.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
if(!radioButton1.isChecked() && !radioButton2.isChecked() && !radioButton3.isChecked() && !radioButton4.isChecked()){
Toast.makeText(QuizActivity.this, "Choose an option before continuing.", Toast.LENGTH_SHORT).show();
}
else if(radioButton1.isChecked()){
Toast.makeText(QuizActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
question++;
question2();
}
else{
Toast.makeText(QuizActivity.this, "Wrong Answer!", Toast.LENGTH_SHORT).show();
answersWrong++;
textViewWrong.setText("Answers Wrong: " + answersWrong);
}
}
});
}
尝试类似的操作。标准做法是将变量缓存在
onCreate
中,因为findViewById
操作非常昂贵:
public class QuizActivity extends ActionBarActivity {
private int question = 1;
private int answersWrong = 0;
private TextView textViewQuestion;
private TextView textViewWrong;
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
private RadioButton radioButton4;
private RadioGroup radioGroup;
private Button buttonNextQ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
textViewQuestion = (TextView) findViewById(R.id.textViewQuestion);
textViewWrong = (TextView) findViewById(R.id.textViewWrong);
radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
radioButton4 = (RadioButton) findViewById(R.id.radioButton4);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
buttonNextQ = (Button) findViewById(R.id.buttonNextQ);
question1();
}
private void question1(){
buttonNextQ.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
if(!radioButton1.isChecked() && !radioButton2.isChecked() && !radioButton3.isChecked() && !radioButton4.isChecked()){
Toast.makeText(QuizActivity.this, "Choose an option before continuing.", Toast.LENGTH_SHORT).show();
}
else if(radioButton1.isChecked()){
Toast.makeText(QuizActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
question++;
question2();
}
else{
Toast.makeText(QuizActivity.this, "Wrong Answer!", Toast.LENGTH_SHORT).show();
answersWrong++;
textViewWrong.setText("Answers Wrong: " + answersWrong);
}
}
});
}
private void question2(){
textViewQuestion.setText("What was the second president, and what were his terms?");
radioButton1.setText("John Adams, 1797 - 1805");
radioButton2.setText("Thomas Jefferson, 1797 - 1801");
radioButton3.setText("Thomas Jefferson, 1797 - 1805");
radioButton4.setText("John Adams, 1797 - 1801");
buttonNextQ.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
if(!radioButton1.isChecked() && !radioButton2.isChecked() && !radioButton3.isChecked() && !radioButton4.isChecked()){
Toast.makeText(QuizActivity.this, "Choose an option before continuing.", Toast.LENGTH_SHORT).show();
}
else if(radioButton1.isChecked()){
Toast.makeText(QuizActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
question++;
//question3();
}
else{
Toast.makeText(QuizActivity.this, "Wrong Answer!", Toast.LENGTH_SHORT).show();
answersWrong++;
textViewWrong.setText("Answers Wrong: " + answersWrong);
}
}
});
}
}
您的字段变量永远不会被忽略assigned@karaokyo我没有分配int字段变量,但分配了others@karaokyo谢谢你澄清,我想这回答了我的问题。我只需要在每个不同的方法中分配所有的变量。这不太对。我遗漏了什么吗?这看起来像是我已经拥有的代码。在question1方法中,将所有“final TextView textViewQuestion=…”替换为“textViewQuestion=…”。当我尝试创建按钮单击侦听器时,它应该会给我一个NullPointerException。你还有一个null的局部变量吗?或者在初始化字段之前调用question1()?