为啥我不能在同一类的受保护方法中调用方法[重复]

Posted

技术标签:

【中文标题】为啥我不能在同一类的受保护方法中调用方法[重复]【英文标题】:Why can't i call a method within a protected method of the same class [duplicate]为什么我不能在同一类的受保护方法中调用方法[重复] 【发布时间】:2018-04-06 11:33:05 【问题描述】:

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

代码---

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 ComponentInfocom.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) 

【问题讨论】:

你需要查看堆栈跟踪以了解崩溃并学习使用调试器 请分享logcat 分享您的代码,包括这两种方法(公共的和受保护的)以及您的 logcat 错误 发布您的代码,不是屏幕截图。发布你的 logcat。 正如它在 logcat 中所说,question 为空。 【参考方案1】:

您正在onCreate 方法中创建button1button2 等的局部变量。这样,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 方法中传递局部变量。

【讨论】:

谢谢 :)

以上是关于为啥我不能在同一类的受保护方法中调用方法[重复]的主要内容,如果未能解决你的问题,请参考以下文章

访问同一类的另一个对象的受保护属性的方法

在 C# 中调用类的受保护构造函数

如何在同一个类的静态方法中访问类的受保护变量?

C#:基类中的受保护方法;无法使用来自另一个类的派生类对象进行访问[重复]

如何从 Derived 内部调用 Base 实例中的受保护成员函数?

为啥python不能解压由winrar使用zip方法创建的受密码保护的zip文件?