为什么我不能在同一个类的受保护方法中调用方法

时间:2017-10-25 11:34:30

标签: java android nullpointerexception oncreate main-activity

在MainActivity类中,我有两个方法 - 一个公共另一个protected.I我从受保护的方法调用公共方法。我正在运行应用程序和应用程序崩溃。如果我在受保护的方法中剪切/粘贴代码一切正常。一种可能性 - 它没有被正确调用。为什么它会崩溃?

代码---

public void generateQuestion(){

   Random rand = new Random();
   int a = rand.nextInt(21);
   int b = rand.nextInt(21);

   question.setText(Integer.toString(a) + "+" + Integer.toString(b));
   int locationOfCorrectAnswer = rand.nextInt(4);

   int incorrectAnswer;

   for (int i = 0; i < 4; i++) {

       if (i == locationOfCorrectAnswer) {
           answers.add(a + b);
       } else {
           incorrectAnswer = rand.nextInt(41);
           while (incorrectAnswer == a + b) {

               incorrectAnswer = rand.nextInt(41);

           }
           answers.add(incorrectAnswer);
       }
   }
   button1.setText(Integer.toString(answers.get(0)));
   button2.setText(Integer.toString(answers.get(1)));
   button3.setText(Integer.toString(answers.get(2)));
   button4.setText(Integer.toString(answers.get(3)));


}




@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    goButton = (Button) findViewById(R.id.goButton);
    question = (TextView) findViewById(R.id.question);
    pagetimer = (TextView) findViewById(R.id.pagetimer);
    noIndiacator = (TextView) findViewById(R.id.noIndiacator);
    resulttext = (TextView) findViewById(R.id.noIndiacator);
    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);
    Button button3 = (Button) findViewById(R.id.button3);
    Button button4 = (Button) findViewById(R.id.button4);

    generateQuestion();

}

的logcat ---

10-25 17:17:57.688 13344-13344/? I/zygote: Not late-enabling -Xcheck:jni (already on)
10-25 17:17:57.699 13344-13344/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
10-25 17:17:58.277 13344-13344/com.example.saurabhdutta.brainteser I/InstantRun: starting instant run server: is main process
10-25 17:17:58.650 13344-13355/com.example.saurabhdutta.brainteser I/zygote: Background concurrent copying GC freed 3526(1172KB) AllocSpace objects, 0(0B) LOS objects, 68% free, 693KB/2MB, paused 1.023ms total 141.970ms
10-25 17:17:58.739 13344-13344/com.example.saurabhdutta.brainteser D/AndroidRuntime: Shutting down VM


                                                                                     --------- beginning of crash
10-25 17:17:58.741 13344-13344/com.example.saurabhdutta.brainteser E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.saurabhdutta.brainteser, PID: 13344
                                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.saurabhdutta.brainteser/com.example.saurabhdutta.brainteser.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                         at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                         at android.os.Looper.loop(Looper.java:164)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
                                                                                         at com.example.saurabhdutta.brainteser.MainActivity.generateQuestion(MainActivity.java:50)
                                                                                         at com.example.saurabhdutta.brainteser.MainActivity.onCreate(MainActivity.java:95)
                                                                                         at android.app.Activity.performCreate(Activity.java:6975)
                                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                                         at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                                         at android.os.Looper.loop(Looper.java:164) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

1 个答案:

答案 0 :(得分:0)

您正在button1方法中创建button2onCreate等的局部变量。这样,generateQuestion方法不知道这些变量,并使用具有相同名称的类变量(不包含在您的代码中,但我想您已经在某处声明了它们可能位于您的活动类之上)初始化,因此你得到NullPointerException

尝试将generateQuestion更改为:

public void generateQuestion(Button button1, Button button2, Button button3, Button button4) {

   Random rand = new Random();
   int a = rand.nextInt(21);
   int b = rand.nextInt(21);

   question.setText(Integer.toString(a) + "+" + Integer.toString(b));
   int locationOfCorrectAnswer = rand.nextInt(4);

   int incorrectAnswer;

   for (int i = 0; i < 4; i++) {

       if (i == locationOfCorrectAnswer) {
           answers.add(a + b);
       } else {
           incorrectAnswer = rand.nextInt(41);
           while (incorrectAnswer == a + b) {

               incorrectAnswer = rand.nextInt(41);

           }
           answers.add(incorrectAnswer);
       }
   }
   button1.setText(Integer.toString(answers.get(0)));
   button2.setText(Integer.toString(answers.get(1)));
   button3.setText(Integer.toString(answers.get(2)));
   button4.setText(Integer.toString(answers.get(3)));


}

并从onCreate这样调用它:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    goButton = (Button) findViewById(R.id.goButton);
    question = (TextView) findViewById(R.id.question);
    pagetimer = (TextView) findViewById(R.id.pagetimer);
    noIndiacator = (TextView) findViewById(R.id.noIndiacator);
    resulttext = (TextView) findViewById(R.id.noIndiacator);
    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);
    Button button3 = (Button) findViewById(R.id.button3);
    Button button4 = (Button) findViewById(R.id.button4);

    generateQuestion(button1, button2, button3, button4);

}

这样您就可以在generateQuestion方法中传递局部变量。