Good afternoon guys, in my application I'm creating a "simple" timer but I'm having trouble executing the application. I created a class that contains two methods one of start and the other one of pause until that is right, in my java activity, instanciei the class and called the method in the onclick of a button and the other onclick called the pause .... while executing my activity it presents me this error ...
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Chronometer.setBase (long)' on a null object reference
From what I've been looking on the internet it seems that my method needs to be long type ... but I do not know how to install a class long, I looked on the internet too and did not know how to solve it, could anyone help me? follow the code below:
class timer:
package com.allsport.miyonic.allsport.Controller;
import android.os.SystemClock;
import android.view.View;
import android.widget.Chronometer;
/**
* Created by nathan on 22/06/2017.
*/
public class TempoJogo {
private View.OnClickListener context;
private Chronometer reloginho;
private long milliseconds;
/*===========Construct==========*/
public TempoJogo(View.OnClickListener context, Chronometer reloginho, long milliseconds){
this.context = context;
this.reloginho = reloginho;
this.milliseconds = milliseconds;
}
public void startJogo(){
/*definindo o milliseconds em zero para poder armazenar o valor que for pausado do systema */
milliseconds = 0;
reloginho.setBase(SystemClock.elapsedRealtime() - milliseconds);
reloginho.start();
}
public void pausarJogo(){
/* pegar último valor armazenado no milliseconds menos o valor atual do systema (0)
retornando o valor atual da activity*/
milliseconds = SystemClock.elapsedRealtime() - reloginho.getBase();
reloginho.stop();
}
}
class activity (SimpleHome):
package com.allsport.miyonic.allsport.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.allsport.miyonic.allsport.Controller.TempoJogo;
import com.allsport.miyonic.allsport.Dialog.DialogFaltaAmarela;
import com.allsport.miyonic.allsport.Dialog.DialogFaltaVermelho;
import com.allsport.miyonic.allsport.Dialog.DialogGol;
import com.allsport.miyonic.allsport.Dialog.DialogInformaction;
import com.allsport.miyonic.allsport.Dialog.DialogSair;
import com.allsport.miyonic.allsport.R;
import com.allsport.miyonic.allsport.Controller.TempoTese;
import Base.*;
public class SimplesHome extends AppCompatActivity {
private ImageButton imgButton_play, imgButton_pause, golCasa, golFora, vermlho, amarelo;
private TempoJogo tempo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_simples);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
golCasa = (ImageButton) findViewById(R.id.golCasaId);
golFora = (ImageButton) findViewById(R.id.golForaId);
imgButton_play = (ImageButton) findViewById(R.id.imgButton_play);
imgButton_pause = (ImageButton) findViewById(R.id.imgButton_pause);
golCasa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
contador++;
valorOne.setText("" + contador);
Context alerta = getApplicationContext();
CharSequence text = "Goooll!";
int duracao = Toast.LENGTH_SHORT;
Toast lert = Toast.makeText(alerta, text, duracao);
lert.show();
DialogGol golzinho = new DialogGol();
golzinho.show(getSupportFragmentManager(), "golzinho_tag");
}
});
golFora.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
contador1++;
valorDouble.setText("" + contador1);
Context gol = getApplication();
CharSequence texto = "Goooll!";
int tempo = Toast.LENGTH_SHORT;
Toast apresentar = Toast.makeText(gol, texto, tempo);
apresentar.show();
DialogGol golzinhoTwo = new DialogGol();
golzinhoTwo.show(getSupportFragmentManager(), "golzinhoTow_tag");
}
});
imgButton_play.setEnabled(true);
imgButton_pause.setEnabled(false);
golCasa.setEnabled(false);
golFora.setEnabled(false);
imgButton_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgButton_play.setEnabled(false);
imgButton_pause.setEnabled(true);
golCasa.setEnabled(true);
golFora.setEnabled(true);
tempo.startJogo();
}
});
imgButton_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgButton_play.setEnabled(true);
imgButton_pause.setEnabled(false);
golCasa.setEnabled(false);
golFora.setEnabled(false);
tempo.pausarJogo();
}
});
}
}
Thank you ....