Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在运行方法内调用方法时出现NullPointerException问题_Java_Android_Nullpointerexception_Android Handler - Fatal编程技术网

Java 在运行方法内调用方法时出现NullPointerException问题

Java 在运行方法内调用方法时出现NullPointerException问题,java,android,nullpointerexception,android-handler,Java,Android,Nullpointerexception,Android Handler,在run方法中调用below方法时,我不断得到一个空指针异常 protected void swTimerDisplay() { String display; long now; long diff; long secs; long mins; long hours; if(timerIsRunning) { now = System.currentTimeMillis(); }else{ now = endTime; } diff = now -

在run方法中调用below方法时,我不断得到一个空指针异常

protected void swTimerDisplay()
{
 String display;
 long now;
 long diff;
 long secs;
 long mins;
 long hours;

 if(timerIsRunning)
 {
     now = System.currentTimeMillis();
 }else{
     now = endTime;
 }

 diff = now - startTime;

 if(diff < 0)
 {
     diff = 0;
 }
 /**
  * Calculate the 
  * secs,mins and hours
  */
 secs = diff / 1000;
 mins = secs  / 60;
 hours = mins / 60;
 secs = secs % 60;
 mins = mins % 60;

 display = String.format("%d", hours) + ":" +
           String.format("%02d", mins) + ":" +
           String.format("%02d", secs);


 swCounter.setText(display);
}
谢谢你能提供的帮助

编辑:

第57行是swCounter.setText(显示)

下面是swTimerDisplay()的位置;被称为

public class timerUpdate extends StopwatchTimer  implements Runnable {

    @Override
    public void run() {
        swTimerDisplay();
        if(handle != null)
        {
            handle.postDelayed(this, UPDATE);
        }

    }

}
希望有人能帮我解决这个问题,因为它让我发疯:)再次感谢

编辑: 这里有更多的代码

public class Stopwatch extends Activity implements OnClickListener {

    private StopwatchTimer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stopwatch);
        //Create new instance of StopwatchTimer
        timer = new StopwatchTimer();
        timer.swCounter = (TextView) findViewById(R.id.swcounter);
        timer.start = (Button) findViewById(R.id.start);
        timer.stop =  (Button) findViewById(R.id.stop);
        timer.start.setOnClickListener(this);
        timer.stop.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if(v==timer.start)
        {
            timer.timerStart();
        }else if(v==timer.stop)
        {
            timer.timerStop();
        }

    }

    public final Runnable timerUpdate = new Runnable(){
        public void run(){
            try {
                timer.swTimerDisplay();
                if(timer.handle != null)
                {
                timer.handle.postDelayed(this, StopwatchTimer.UPDATE); 
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }   
        }
    };



}
上面是我的代码,在run方法中调用了swTimerDisplay,如果您能提供调用swTimerDisplay的类,我们需要知道“第57行”是什么。没有这个,从这里我只能看到两种可能性,如果你还没有看到,你应该检查一下

  • 您对StopwatchTimer的引用为空,即尚未实例化, 调用
    swTimerDisplay()
    时。实际上,您将执行
    null.swTimerDisplay()
  • swTimerDisplay()
    的最后一行中,
    swCounter
    为空

  • 编辑:我刚刚注意到你的方法是受保护的,所以我要看第2点。

    timerIsRunning是布尔值吗?或布尔值?对
    swCounter
    的引用是否为
    null
    ?哪一行是StopwatchTimer.java:57呢?在哪里声明了
    endTime
    呢?哪一行是第57行?如果有的话,使用调试器设置断点和检查变量可能比玩“猜空变量”游戏更成功。调试器很好,因为你不会猜。第57行是swCounter.setText(display);我在我的帖子中添加了额外的细节。感谢againOk,我用if语句检查了swCounter,它确实为null。但是我在onCreate方法中使用findviewbyd设置了swCounter,所以idk为什么null@user3325917你能粘贴更多的代码吗?例如onCreate,并指出与此相关的swTimerDisplay()调用位置?在哪里添加Runnable?您可能有一个竞争条件:。还要再次检查您的
    id
    是否正确。好的,我在我的帖子的第二次编辑中添加了额外的代码。至于id im 100%阳性,它是正确的,因为我使用了setText来测试它,它工作了。
    public class Stopwatch extends Activity implements OnClickListener {
    
        private StopwatchTimer timer;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.stopwatch);
            //Create new instance of StopwatchTimer
            timer = new StopwatchTimer();
            timer.swCounter = (TextView) findViewById(R.id.swcounter);
            timer.start = (Button) findViewById(R.id.start);
            timer.stop =  (Button) findViewById(R.id.stop);
            timer.start.setOnClickListener(this);
            timer.stop.setOnClickListener(this);
        }
    
    
        @Override
        public void onClick(View v) {
            if(v==timer.start)
            {
                timer.timerStart();
            }else if(v==timer.stop)
            {
                timer.timerStop();
            }
    
        }
    
        public final Runnable timerUpdate = new Runnable(){
            public void run(){
                try {
                    timer.swTimerDisplay();
                    if(timer.handle != null)
                    {
                    timer.handle.postDelayed(this, StopwatchTimer.UPDATE); 
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }   
            }
        };
    
    
    
    }