第二次 Runnable 中的 Android NullPointerException [英] Android NullPointerException in Runnable for the second time

查看:55
本文介绍了第二次 Runnable 中的 Android NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这个错误是因为我不完全理解线程,但是这里...

I'm sure this error is because I don't fully understand threads, but here it goes...

我有一个 runnable,它在方法中调用 onCreate() 时启动:

I have a runnable that is started when onCreate() is called within a method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    //Set all app specific starting points here
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_avatar);
    ...
    soundMeterLoop();
}  

public void soundMeterLoop() {
    Log.d("SpeechKit", "Start Sound Meter");
    soundMeterHandler = new Handler();
    soundMeterRunnable = new Runnable() {
        @Override
        public void run() {
            if(!soundMeter.SoundMeterRunning) {
                Log.d("SpeechKit", "Start SoundMeter in the runnable");
                startSoundMeter();
            }
            if (soundMeter.mMediaRecorder != null) {
                amplitude = soundMeter.getAmplitude();
                decibelLevelOutput.setText("" + amplitude);
                if (amplitude > threshold) {
                    decibelLevelOutput.setTextColor(Color.RED);
                    Log.d("SpeechKit", "Interrupt and run startNuance()");
                    startNuance();
                } else {
                    decibelLevelOutput.setTextColor(Color.BLACK);
                    Log.d("SpeechKit", "Running");
                    soundMeterHandler.postDelayed(this, 100);
                }
            }

        }
    };
    soundMeterHandler.postDelayed(soundMeterRunnable, 100);
}

当它在 onCreate 中创建时运行得很好.如您所见,它会自行终止(如果语句失败则不更新循环)并运行 startNuance().

This runs just fine when it's created in the onCreate. As you can see, it kills itself (by not renewing the loop if the statement fails) and runs startNuance().

public void startNuance() {
    soundMeterHandler.removeCallbacksAndMessages(soundMeterRunnable);
    nuance.toggleReco();
}

然后我杀死可运行对象并在另一个类中启动一个方法.这个类运行良好,然后当它完成它的工作时,我用 avatar.stopNuance();

I then kill the runnable and start a method in another class. This class runs fine, then when it's finished doing its thing, I call back to this main class with avatar.stopNuance();

这是在 Nuance.java 类中

This is in the Nuance.java class

    @Override
    public void onFinishedRecording(Transaction transaction) {
        Log.d("SpeechKit", "onFinishedRecording");
        //We have finished recording the users voice.
        //We should update our state and stop polling their volume.
        state = State.PROCESSING;
        stopAudioLevelPoll();
        avatar.stopNuance();  // <<<<<
    }

然后它返回到我的主要活动(avatar)并运行这个 stopNuance() 方法:

It then returns back to my main activity (avatar) and runs this stopNuance() method:

public void stopNuance() {
    Log.d("SpeechKit", "stopNuance(), start loop again");
    soundMeterLoop();
}

然后它尝试运行与之前相同的循环.只是这一次,我收到了很多与空指针异常有关的错误.具体以decibelLevelOutput.setText("" +amplitude);

Then it tries to run the same loop from before. Only this time, I'm getting a lot of errors that pertain to nullpointerexceptions. specifically starting with decibelLevelOutput.setText("" + amplitude);

我不知道为什么这些东西是 null 或者如何修复它们.是不是因为在runnable的创建中启动了一个没有启动的新线程?

I'm not sure why these things are null or how to fix them. Is this because it started a new thread that was not started in the creation of the runnable?

推荐答案

在聊天中讨论后,实际问题在代码库的其他地方.

After talking on chat the actual issue was elsewhere in the codebase.

问题是这样的:

public class Nuance {

      private Activity activity;
      private Session session;
      public Avatar avatarActivity = new Avatar(); // DONT DO THIS

 ....

         @Override
         public void onFinishedRecording(Transaction transaction) {
             Log.d("SpeechKit", "onFinishedRecording");
             //We have finished recording the users voice.
              //We should update our state and stop polling their volume.
              state = State.PROCESSING;
              stopAudioLevelPoll();
             avatarActivity.stopNuance();
          }

您永远不应该创建自己的 Activity 实例.它们由系统生成和管理.系统将调用实例上的生命周期方法(onCreate 等),但如果您创建一个实例,则不会调用这些方法 - 因此会发生很多奇怪的行为.

You should never, ever ever create your own instance of an Activity. They are creted and managed by the system. The system will call the lifecycle methods on the instance (onCreate etc) but if you create an instance these methods are not called - therefore a lot of strange behaviour happens.

这里的修复是这样的:

public class Nuance {

      private Avatar activity;
      private Session session;

 ....

         @Override
         public void onFinishedRecording(Transaction transaction) {
             Log.d("SpeechKit", "onFinishedRecording");
             //We have finished recording the users voice.
              //We should update our state and stop polling their volume.
              state = State.PROCESSING;
              stopAudioLevelPoll();
              activity.stopNuance();
          }

这篇关于第二次 Runnable 中的 Android NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆