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

Why can't i call a method within a protected method of the same class

在 MainActivity 中 class 我有两种方法 - 一种 public 另一种 protected.I 正在从受保护的 method.I 调用 public 方法 运行 应用程序和应用程序 crashes.If 我 cut/paste 受保护方法中的代码一切正常 fine.One 可能性 - 它没有被调用 properly.Why 它崩溃了吗?

代码---

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) 

您正在 onCreate 方法中创建 button1button2 等局部变量。这样,generateQuestion 方法不知道这些变量并使用具有相同名称的 class 变量(不包含在您的代码中,但我想您可能在某处声明了它们可能在您的 activity class) 没有初始化,因此你得到 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 方法中传递局部变量。