0
Good afternoon guys, in my application I am creating a "simple" chronometer but I’m having trouble running application. I created a class that contains two methods one of start and the other of pause even then it is right, in my Java App, I instated the class and called the method in the onclick of a boot and the other onclick called pause.... by executing my Activity he presents me with 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 the long type... but I don’t know how to install a long class, I looked on the internet too and did not know how to solve, Could someone help me? follows the code below:
chronometer class:
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 (Simpleshome):
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....
On which line of code is the error?
– Márcio Oliveira
In this line:
reloginho.setBase(SystemClock.elapsedRealtime() - milliseconds);
– Nathan
@Nathan, you don’t seem to have introduced the clock variable, by the code.
– Junior Porfirio
I made a change to the code
– Nathan